如何在 Entity Framework 6 中使用 Web Api 在 Asp.Net 中实现交易?

How to implement transaction in Entity Framework 6 with Web Api in Asp.Net?

有什么方法可以在 Entity Framework 6 中使用 Web API 在 Asp.Net 中实现交易吗?

我在单个 asp.net Web 表单中使用 Web API 调用分别插入 10 个表。我正在使用 Entity framework 和 Web API.

寻求想法或技术可行性建议

切记:

In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.

如果你真的想执行交易,那真的很简单:

using (var context = new SomeDbContext())
{
 using (DbContextTransaction transaction = context.Database.BeginTransaction()) {
   //do stuff
   context.SaveChanges();
   // multiple saves
   context.SaveChanges();
   transaction.Commit(); // this is one transaction
 }
}

是的。

您可以使用 EF 提供的上下文来创建事务范围。在范围的末尾,您可以提交或回滚。

你可以这样做:

class WebController
{
    public Response restMethod()
    {
        var context = getYourDBCOntext();
        using(var dbContextTransaction = context.Database.BeginTransaction())
        {
            try {
                // do something with the DB
                context.Database.ExecuteSqlCommand( /* sql command */ );

                // save changes
                context.SaveChanges();
                // commit transaction
                dbContextTransaction.Commit();
            catch(Exception)
            {
                // Rollback in case of an error
                dbContextTransaction.Rollback();
            }
        }
    }
}

下面是示例代码片段,可以清楚地说明多个 table 的创建或更新事务。第一个 table 列 ID 是其他子 table 的外键。因此,如果子 table 插入出现异常,父 table 记录也将回滚。因此,包含在事务中的整个 table 将成功回滚。

    public bool CreateOrUpdateEmployee(Common common)
    {
        bool IstransactionComplete= false;
        EmployeeEntities DbContext = new EmployeeEntities();

        using (var transaction = DbContext.Database.BeginTransaction())
        {
            try
            {
                if (common.Mode == Modes.CREATE) //Modes - User defined Enum
                {
                    DbContext = CreateFinanceEmployees(common, DbContext); //DbContext.savechanges() inside this method.

                    DbContext = CreateManufacturingEmployee(common, DbContext); //DbContext.savechanges() inside this method.

                    DbContext = CreateLogisticsEmployee(common, DbContext);  //DbContext.savechanges() inside this method.
                }
                else
                {
                    DbContext = UpdateFinanceEmployees(common, DbContext);  //DbContext.savechanges() inside this method.

                    DbContext = UpdateManufacturingEmployee(common, DbContext);  //DbContext.savechanges() inside this method.

                    DbContext = UpdateLogisticsEmployee(common, DbContext);  //DbContext.savechanges() inside this method.
                }

                **transaction.Commit();**

                IstransactionComplete=true;
            }
            catch (Exception ex)
            {
                **transaction.Rollback();**

                IstransactionComplete=false;
            }
            finally
            {
                transaction.Dispose();
            }
        }
        return IstransactionComplete;
    }