Loading

entity framework5 sqlserver2005 事务(TransactionScope)报未启用MSDTC错误解决办法

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

 

posted @ 2012-12-18 15:29  chear  阅读(467)  评论(0编辑  收藏  举报