LINQ to Entities 事务简单例子

默认LINQ to Entities会使用隐式事务,即:对于每一个savechanges都分开在单独的事务之中。

 

也可以显式地指定事务:

 using (var db = new TestEntities())
            {
                using (TransactionScope ts = new TransactionScope())
                {

                    var call1 = db.Call.FirstOrDefault();
                    call1.Content = "123321";
                    db.SaveChanges();

                    var call2 = db.Call.FirstOrDefault();
                    call2.Fax = "new faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew faxnew fax";
                    db.SaveChanges();

                }
            }

在配置msdtc后,当call2.Fax过长时,事务将不会提交。

posted @ 2014-01-17 12:08  瓜王  阅读(280)  评论(0编辑  收藏  举报