ASP.NET MVC 5 + Entity Framework 5 – Transaction Handling
using System; using System.Linq; namespace InventoryDAO.DAO.Inventory { public class AuthDAO { private InventoryEntities entity; public AuthDAO() { this.entity = new InventoryEntities(); } … … public void setUser(SystemUser user) { using (var dbContextTransaction = this.entity.Database.BeginTransaction()) { try { this.entity.SystemUsers.Add(user); this.entity.SaveChanges(); … … this.entity.SaveChanges(); … … this.entity.SaveChanges(); dbContextTransaction.Commit(); } catch(Exception ex) { dbContextTransaction.Rollback(); //Console.WriteLine(ex.Message); } } } } } |