在Entity Framework Core的不同方法中使用相同的事务

问题描述 投票:4回答:3

编辑(02/03/2018):自实体框架核心2.1以来,EF Core实现了事务,跨上下文事务,环境事务和事务范围,因此这个问题现在已经过时了。

这是关于EF Core中交易的官方文档:https://docs.microsoft.com/en-us/ef/core/saving/transactions


如何在不同的方法中使用相同的事务?目标是在发生错误时提交或回滚所有修改。

我正在使用Entity Framework Core版本1.1.0-preview1-final和SQL Server 2014。

例如,我有一个Entity Framework数据库上下文:

public class ApplicationDatabaseContext : DbContext
    {
        public ApplicationDatabaseContext(DbContextOptions<ApplicationDatabaseContext> options)
           : base(options)
        { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TransactionLog1>(entity =>
            {
                entity.ToTable("TRANSACTION_LOG_1");

                entity.Property(e => e.CreationDate)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("getdate()");
            });

            modelBuilder.Entity<TransactionLog2>(entity =>
            {
                entity.ToTable("TRANSACTION_LOG_2");

                entity.Property(e => e.CreationDate)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("getdate()");
            });
        }

        public virtual DbSet<TransactionLog1> TransactionLog1 { get; set; }
        public virtual DbSet<TransactionLog2> TransactionLog2 { get; set; }
    }

我有两个类来处理数据,两者都使用相同的上下文:

public interface IRepository1
{
    void Create(Guid key);
}

public sealed class Repository1 : IRepository1
{
    private readonly ApplicationDatabaseContext _dbContext;

    public Repository1(ApplicationDatabaseContext dbcontext)
    {
        _dbContext = dbcontext;
    }

    public void Create(Guid key)
    {
        using (_dbContext.Database.BeginTransaction())
        {
            try
            {
                _dbContext.TransactionLog1.Add(new TransactionLog1 { Key = key });
                _dbContext.SaveChanges();

                _dbContext.Database.CommitTransaction();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

public interface IRepository2
{
    void Create(Guid key);
}

public sealed class Repository2 : IRepository2
{
    private readonly ApplicationDatabaseContext _dbContext;

    public Repository2(ApplicationDatabaseContext dbcontext)
    {
        _dbContext = dbcontext;
    }

    public void Create(Guid key)
    {
        using (_dbContext.Database.BeginTransaction())
        {
            try
            {
                _dbContext.TransactionLog2.Add(new TransactionLog2 { Key = key });
                _dbContext.SaveChanges();

                _dbContext.Database.CommitTransaction();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

在我的业务逻辑中,我有一个服务,我想在我的第一个存储库上调用方法void Create(Guid key),然后从我的第二个存储库调用相同的方法,并且只有在两者都没有发生错误时才提交(如果发生任何错误)在secon方法中,我想回滚在第一个方法中完成的提交)。

我怎样才能做到这一点 ? Entity Framework Core和交易的最佳实践是什么?

我试过几件事,比如这样,但它永远不会有效(用这种方法我有错误):

作为警告'RelationalEventId.AmbientTransactionWarning'的错误异常警告:已检测到环境事务。实体框架核心不支持环境事务。

public sealed class Service3 : IService3
{
        private readonly IRepository1 _repo1;
        private readonly IRepository2 _repo2;

        public Service3(IRepository1 repo1, IRepository2 repo2)
        {
            _repo1 = repo1;
            _repo2 = repo2;
        }

        public void Create(Guid key)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    _repo1.Create(key);
                    _repo2.Create(key);

                    scope.Complete();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
}

我阅读了文档,特别是这个页面(https://docs.microsoft.com/en-us/ef/core/saving/transactions),但我没有在Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade上使用UseTransaction方法。

c# transactions entity-framework-core asp.net-core-1.0 sql-server-2014-express
3个回答
5
投票

编辑(02/03/2018):自实体框架核心2.1以来,您可以使用事务,跨上下文事务,环境事务和事务范围,因此您不必实现解决方法。

这是官方文件:https://docs.microsoft.com/en-us/ef/core/saving/transactions


我终于找到了一个等待下一个Entity Framework Core版本的解决方案,它将支持使用事务范围和环境事务。

由于db事务与数据库上下文相关,并且数据库上下文在我的所有数据访问类中都是相同的(感谢依赖注入),当我在进程中启动事务时,它将被其他数据访问类共享。交易将被处置之前的相同过程(我必须将我的实体框架核心升级到1.1.0-preview1-final以进行一次性交易)。

具体来说,我有一个类来处理交易:

public interface ITransactionDealerRepository
{
    void BeginTransaction();

    void CommitTransaction();

    void RollbackTransaction();

    void DisposeTransaction();
}

public sealed class TransactionDealerRepository : BaseEntityFrameworkRepository, ITransactionDealerRepository
{
    public TransactionDealerRepository(MyDBContext dbContext)
       : base(dbContext)
    { }

    public void BeginTransaction()
    {
        _dbContext.Database.BeginTransaction();
    }

    public void CommitTransaction()
    {
        _dbContext.Database.CommitTransaction();
    }

    public void RollbackTransaction()
    {
        _dbContext.Database.RollbackTransaction();
    }

    public void DisposeTransaction()
    {
        _dbContext.Database.CurrentTransaction.Dispose();
    }
}

我在我的服务中使用这个类:

    public void Create(Guid key)
    {
        _transactionProvider.BeginTransaction();

        try
        {
            _repo1.Create(key);
            _repo2.Create(key);

            _transactionProvider.CommitTransaction();
        }
        catch (Exception)
        {
            _transactionProvider.RollbackTransaction();
            throw;
        }
        finally
        {
            _transactionProvider.DisposeTransaction();
        }
    }

4
投票

一种可能的方法是使用中间件并将逻辑用于开始/提交/回滚。例如,在每个请求开始时,您将在基础数据库连接上开始事务。在请求结束时提交或回滚事务。由于您最有可能每个请求使用单个上下文实例,这将解决您的问题。此外,您将从存储库/服务类中提取此问题。

以下是您可能用作启动的示例代码。但是没有在真实场景中测试它:

public class TransactionPerRequestMiddleware
{
    private readonly RequestDelegate next_;

    public TransactionPerRequestMiddleware(RequestDelegate next)
    {
        next_ = next;
    }

    public async Task Invoke(HttpContext context, ApplicationDbContext dbContext)
    {
        var transaction = dbContext.Database.BeginTransaction(
            System.Data.IsolationLevel.ReadCommitted);

        await next_.Invoke(context);

        if (context.Response.StatusCode == 200)
        {
            transaction.Commit();
        }
        else
        {
            transaction.Rollback();
        }
    }
}

然后在你的Startup.Configure()方法:

app.UseMiddleware<TransactionPerRequestMiddleware>();

0
投票

一旦实体框架本身将每个请求包装为事务,您就可以避免显式事务和显式“saveChanges”,并且您将获得所有提交的请求或原子回滚

© www.soinside.com 2019 - 2024. All rights reserved.