mirror of https://github.com/abpframework/abp.git
6 changed files with 59 additions and 91 deletions
@ -1,46 +0,0 @@ |
|||||
using System.Data; |
|
||||
using Microsoft.EntityFrameworkCore; |
|
||||
using Microsoft.EntityFrameworkCore.Storage; |
|
||||
using Microsoft.Extensions.DependencyInjection; |
|
||||
using Volo.Abp.DependencyInjection; |
|
||||
using Volo.Abp.EntityFrameworkCore; |
|
||||
using Volo.Abp.EntityFrameworkCore.DependencyInjection; |
|
||||
|
|
||||
namespace Volo.Abp.Uow.EntityFrameworkCore |
|
||||
{ |
|
||||
public class DbContextEfCoreTransactionStrategy : IEfCoreTransactionStrategy, ITransientDependency |
|
||||
{ |
|
||||
public TDbContext CreateDbContext<TDbContext>(IUnitOfWork unitOfWork, DbContextCreationContext creationContext) where TDbContext : DbContext |
|
||||
{ |
|
||||
var transactionApiKey = $"EntityFrameworkCore_{creationContext.ConnectionString}"; |
|
||||
var activeTransaction = unitOfWork.FindTransactionApi(transactionApiKey) as IEfCoreTransactionApi; |
|
||||
|
|
||||
TDbContext dbContext; |
|
||||
if (activeTransaction == null) |
|
||||
{ |
|
||||
dbContext = unitOfWork.ServiceProvider.GetRequiredService<TDbContext>(); |
|
||||
var dbtransaction = dbContext.Database.BeginTransaction((unitOfWork.Options.IsolationLevel ?? IsolationLevel.ReadUncommitted)); |
|
||||
activeTransaction = new EfCoreTransactionApi(dbtransaction, dbContext); |
|
||||
unitOfWork.AddTransactionApi(transactionApiKey, activeTransaction); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
creationContext.ExistingConnection = activeTransaction.DbContextTransaction.GetDbTransaction().Connection; |
|
||||
dbContext = unitOfWork.ServiceProvider.GetRequiredService<TDbContext>(); |
|
||||
|
|
||||
if (dbContext.HasRelationalTransactionManager()) |
|
||||
{ |
|
||||
dbContext.Database.UseTransaction(activeTransaction.DbContextTransaction.GetDbTransaction()); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
dbContext.Database.BeginTransaction(); |
|
||||
} |
|
||||
|
|
||||
activeTransaction.AttendedDbContexts.Add(dbContext); |
|
||||
} |
|
||||
|
|
||||
return dbContext; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,15 +0,0 @@ |
|||||
using System.Collections.Generic; |
|
||||
using Microsoft.EntityFrameworkCore; |
|
||||
using Microsoft.EntityFrameworkCore.Storage; |
|
||||
|
|
||||
namespace Volo.Abp.Uow.EntityFrameworkCore |
|
||||
{ |
|
||||
public interface IEfCoreTransactionApi : ITransactionApi |
|
||||
{ |
|
||||
IDbContextTransaction DbContextTransaction { get; } |
|
||||
|
|
||||
DbContext StarterDbContext { get; } |
|
||||
|
|
||||
List<DbContext> AttendedDbContexts { get; } |
|
||||
} |
|
||||
} |
|
||||
@ -1,11 +0,0 @@ |
|||||
using Microsoft.EntityFrameworkCore; |
|
||||
using Volo.Abp.EntityFrameworkCore.DependencyInjection; |
|
||||
|
|
||||
namespace Volo.Abp.Uow.EntityFrameworkCore |
|
||||
{ |
|
||||
public interface IEfCoreTransactionStrategy |
|
||||
{ |
|
||||
TDbContext CreateDbContext<TDbContext>(IUnitOfWork unitOfWork, DbContextCreationContext creationContext) |
|
||||
where TDbContext : DbContext; |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue