# Monday, March 23, 2009

Entity Framework: ObjectContext.SaveChanges is transactional

The documentation doesn't specifiy it but in the Entity Framework when you call ObjectContext.SaveChanges the update is 'wrapped' in a transaction.

NorthwindIBModel model = new NorthwindIBModel();

Guid id = Guid.NewGuid();

model.AddToCustomer(new Customer() { CustomerID = id, ContactName = "Andrew", CompanyName = "Northwind Traders" });
model.AddToCustomer(new Customer() { CustomerID = id, ContactName = "Aikido", CompanyName = "Northwind Traders " });

model.SaveChanges(); // exception duplicate key - transactional -> no changes to the database

#    Comments [0] |