SaveChanges() for multiple dbcontext with TransactionScope in Entity Framework 4.1
.net, asp.net-mvc-3-areas, c#, entity-framework, entity-framework-4
Solution
Try this:
using (TransactionScope scope = new TransactionScope())
{
// Save changes but maintain context1 current state.
context1.SaveChanges(SaveOptions.DetectChangesBeforeSave);
// Save changes but maintain context2 current state.
context2.SaveChanges(SaveOptions.DetectChangesBeforeSave);
// Commit succeeded since we got here, then completes the transaction.
scope.Complete();
// Now it is safe to update context state.
context1.AcceptAllChanges();
context2.AcceptAllChanges();
}
This sample was taken from this blog post:
Managing Transactions with Entity Framework 4
Problem
I am using `SaveChanges()` method as given below: `objAdbContext` of Database `A` `objBdbContext` of Database `B` Updating table of DB A as given below ``` public string SaveA() { //Some stuff objAdbContext.SaveChanges(); string result=UpdateDatabaseB(some parameters) //Some stuff } public string UpdateDatabaseB(some parameters) { //Some stuff objBdbContext.SaveChanges(); return "Success"; } ``` This case Database B is not getting updated. Is it correct way of updating multiple databases? Both are independent Databases and How to implement TransactionScope in this case?