详情请看:http://stackoverflow.com/questions/12809958/ef-how-do-i-call-savechanges-twice-inside-a-transaction
using (var transaction = new TransactionScope()) { // Do something db.SaveChanges(); // Do something else db.SaveChanges(); tramsaction.Complete(); }
使用上面代码会报数据库没有启用MSDTC。。
---------------------------------------------
解决方法:
var objectContext = ((IObjectContextAdapter)db).ObjectContext; try { objectContext.Connection.Open(); using (var transaction = new TransactionScope()) { // Do something db.SaveChanges(); // Do something else db.SaveChanges(); tramsaction.Complete(); } } finally { objectContext.Connection.Close(); }