详情请看: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();
} 

 

相关文章:

  • 2022-02-11
  • 2022-12-23
  • 2021-12-02
  • 2022-02-16
  • 2022-01-02
  • 2022-12-23
猜你喜欢
  • 2021-07-11
  • 2021-11-30
  • 2022-12-23
  • 2021-12-25
  • 2021-08-21
相关资源
相似解决方案