C# 如何在实体框架中应用事务

C# 如何在实体框架中应用事务,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我有两张桌子。我正在使用实体框架更新这些表。这是我的密码 public bool UpdateTables() { UpdateTable1(); UpdateTable2(); } 如果任何表更新操作失败,则不应提交其他操作。如何在实体框架中实现此操作?使用transactionscope using (TransactionScope transaction = new TransactionScope()) { bool success = false;

我有两张桌子。我正在使用实体框架更新这些表。这是我的密码

public bool UpdateTables()
{
      UpdateTable1();
      UpdateTable2();
}
如果任何表更新操作失败,则不应提交其他操作。如何在实体框架中实现此操作?

使用transactionscope

using (TransactionScope transaction = new TransactionScope())
{
    bool success = false;
    try
    {
        //your code here
        UpdateTable1();
        UpdateTable2();
        transaction.Complete();
        success = true;
    }
    catch (Exception ex)
    {
        // Handle errors and deadlocks here and retry if needed.
        // Allow an UpdateException to pass through and 
        // retry, otherwise stop the execution.
        if (ex.GetType() != typeof(UpdateException))
        {
            Console.WriteLine("An error occured. "
                + "The operation cannot be retried."
                + ex.Message);
            break;
        }
    }    

    if (success)
        context.AcceptAllChanges();
    else    
        Console.WriteLine("The operation could not be completed");

    // Dispose the object context.
    context.Dispose();    
}
   public bool UpdateTables()
    {
        using (System.Transactions.TransactionScope sp = new System.Transactions.TransactionScope())
        {
            UpdateTable1();
            UpdateTable2();
            sp.Complete();
        }
    }

此外,您还需要将System.Transactions添加到项目参照中,您不需要使用TransactionScope:Entity Framework在您的上下文中调用SaveChanges()时自动强制执行事务

public bool UpdateTables()
{
    using(var context = new MyDBContext())
    {
        // use context to UpdateTable1();
        // use context to UpdateTable2();

        context.SaveChanges();
    } 
}

你可以这样做

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead }))
                {
                    using (YeagerTechEntities DbContext = new YeagerTechEntities())
                    {
                        Category category = new Category();

                        category.CategoryID = cat.CategoryID;
                        category.Description = cat.Description;

                        // more entities here with updates/inserts
                        // the DbContext.SaveChanges method will save all the entities in their corresponding EntityState

                        DbContext.Entry(category).State = EntityState.Modified;
                        DbContext.SaveChanges();

                        ts.Complete();
                    }
                }

谢谢你的快速回复。我正在使用WCF服务,它使用实体框架执行数据库操作。在这种情况下这个方法行吗?是的,TransactionScope使代码块具有事务性(在任何地方使用)。TransactionScope确保对对象上下文中对象的更改与消息队列相协调;语句,以防止事务回滚?抱歉,我忘记了。感谢Anton指出:)请注意Ralph Lavelle的回答:您不需要使用事务!SaveChanges方法本身处理事务!至少在这个例子中,使用TransactionScope是完全没有意义的!只需在dbContext对象上调用SaveChanges方法。除非在多次调用SaveChanges方法的场景中。