How to use transactions with the Entity Framework?

entity-framework, transactions

Solution

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).

Problem

When you have code like this: ``` Something something = new Something(); BlahEntities b = new BlahEntities() b.AddToSomethingSet(something); b.SaveChanges(); ``` how do run that addition inside a transaction?

Original source