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();

} 

 

你可能感兴趣的:(sqlserver2005)