mirror of https://github.com/abpframework/abp
commit
1835c37d63
Binary file not shown.
@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
|
||||
namespace Volo.Abp.EntityFrameworkCore
|
||||
{
|
||||
internal static class DbContextExtensions
|
||||
{
|
||||
public static bool HasRelationalTransactionManager(this DbContext dbContext)
|
||||
{
|
||||
return dbContext.Database.GetService<IDbContextTransactionManager>() is IRelationalTransactionManager;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Threading;
|
||||
|
||||
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection
|
||||
{
|
||||
public class DbContextCreationContext
|
||||
{
|
||||
public static DbContextCreationContext Current => _current.Value;
|
||||
private static readonly AsyncLocal<DbContextCreationContext> _current = new AsyncLocal<DbContextCreationContext>();
|
||||
|
||||
public string ConnectionStringName { get; }
|
||||
|
||||
public string ConnectionString { get; }
|
||||
|
||||
public DbConnection ExistingConnection { get; set; }
|
||||
|
||||
public DbContextCreationContext(string connectionStringName, string connectionString)
|
||||
{
|
||||
ConnectionStringName = connectionStringName;
|
||||
ConnectionString = connectionString;
|
||||
}
|
||||
|
||||
public static IDisposable Use(DbContextCreationContext context)
|
||||
{
|
||||
var previousValue = Current;
|
||||
_current.Value = context;
|
||||
return new DisposeAction(() => _current.Value = previousValue);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Volo.Abp.EntityFrameworkCore;
|
||||
|
||||
namespace Volo.Abp.Uow.EntityFrameworkCore
|
||||
{
|
||||
public class EfCoreTransactionApi : ITransactionApi, ISupportsRollback
|
||||
{
|
||||
public IDbContextTransaction DbContextTransaction { get; }
|
||||
public DbContext StarterDbContext { get; }
|
||||
public List<DbContext> AttendedDbContexts { get; }
|
||||
|
||||
public EfCoreTransactionApi(IDbContextTransaction dbContextTransaction, DbContext starterDbContext)
|
||||
{
|
||||
DbContextTransaction = dbContextTransaction;
|
||||
StarterDbContext = starterDbContext;
|
||||
AttendedDbContexts = new List<DbContext>();
|
||||
}
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
DbContextTransaction.Commit();
|
||||
|
||||
foreach (var dbContext in AttendedDbContexts)
|
||||
{
|
||||
if (dbContext.HasRelationalTransactionManager())
|
||||
{
|
||||
continue; //Relational databases use the shared transaction
|
||||
}
|
||||
|
||||
dbContext.Database.CommitTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
public Task CommitAsync()
|
||||
{
|
||||
Commit();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
DbContextTransaction.Dispose();
|
||||
}
|
||||
|
||||
public void Rollback()
|
||||
{
|
||||
DbContextTransaction.Rollback();
|
||||
}
|
||||
|
||||
public Task RollbackAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
DbContextTransaction.Rollback();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<AssemblyName>Volo.Abp.EntityFrameworkCore.Tests</AssemblyName>
|
||||
<PackageId>Volo.Abp.EntityFrameworkCore.Tests</PackageId>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
<RootNamespace />
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
|
||||
<ProjectReference Include="..\..\src\Volo.Abp.EntityFrameworkCore\Volo.Abp.EntityFrameworkCore.csproj" />
|
||||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" />
|
||||
<ProjectReference Include="..\Volo.Abp.TestApp\Volo.Abp.TestApp.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Volo\Abp\TestApp\EntityFrameworkCore\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in new issue