1.
using (Db1Context db = new Db1Context()) { using (var tran = db.Database.BeginTransaction()) { //db operation result = db.SaveChanges(); using (Db2Context db2 = new Db2Context(db.Database.Connection,false))//必须false { db2.Database.UseTransaction(db.Database.GetDbTransaction()); //db2 operation db2.SaveChanges(); } tran.Commit(); } }
2. TransactionScope