How to use transactions with the Entity Framework?

前端 未结 5 493
说谎
说谎 2020-12-13 13:47

When you have code like this:

Something something = new Something();
BlahEntities b = new BlahEntities()    
b.AddToSomethingSet(something);
b.SaveChanges();         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 14:33

    You can place your code within a Transaction scope

    using(TransactionScope scope = new TransactionScope())
    {
        // Your code
        scope.Complete(); //  To commit.
    }
    

    TransactionScope is in the System.Transactions namespace which is located in the assembly of the same name (which you may need to add manually to your project).

提交回复
热议问题