30% OFF - 10th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY10
Entity Framework Transaction Discover How Managing a Trans When Saving
How Entity Framework Transaction Works?
How Entity Framework Transaction Works and how to use multiple database operations within a single transaction.
StackOverflow Related Questions
Answer
In Entity Framework 6, when you call SaveChanges() to insert, delete, or update data to the database, then entity framework will wrap that operation in a transaction.
- SaveChanges automatically starts a transaction and commits or rolls it back.
- It means the Entity Framework maintains a transaction for the multiple entity inserts, update and delete in a single SaveChanges() method.
- When we execute another operation, the Entity Framework creates a new transaction.
In EF 6 and EF Core, you can use multiple SaveChanges within a single transaction as shown below;
using (var context = new EntityContext()) { context.Database.Log = Console.Write; using (DbContextTransaction transaction = context.Database.BeginTransaction()) { try { List<Customer> customerList = new List<Customer>() { new Customer() { Name ="John"}, new Customer() { Name ="Andy"}, new Customer() { Name ="Mark"} }; context.Customers.AddRange(customerList); context.SaveChanges(); context.Invoices.Add(new Invoice() { Date = DateTime.Now.AddDays(-107), CustomerID = 1}); context.SaveChanges(); } catch (Exception ex) { transaction.Rollback(); } } }
ZZZ Projects