testModel.testEntities1 test = new testModel.testEntities1();        //test.Connection.Open();        //test.ExecuteStoreCommand("insert into userinfo (id,username,password) values(44554,'aaa','dddd')");        //System.Data.Common.DbTransaction trans = test.Connection.BeginTransaction();        // UserInfo user = new UserInfo { id = "111111111", password = "a", username = "a" };        UserInfo user1 = new UserInfo { id = "111144111", password = "ddd", username = "地对地导弹" };        Email em = new Email { emal = "dd", userid = "111111111", id = "dd" };        // test.AddObject("UserInfo", user);        test.AddObject("Email", em);        test.AddObject("UserInfo", user1);        test.SaveChanges().ToString();//对相同表,还是不相同表操作都是事务        //trans.Commit();        //testModel.testEntities1 test = new testModel.testEntities1();        //test.Connection.Open();        //test.ExecuteStoreCommand("insert into userinfo (id,username,password) values(44554,'aaa','dddd')");        //System.Data.Common.DbTransaction trans = test.Connection.BeginTransaction();        //UserInfo user = new UserInfo { id = "23333332", password = "ddd", username = "dddd" };        //test.AddObject("UserInfo", user);        //Response.Write(test.SaveChanges().ToString());        //trans.Commit();
Entity Framework中的批量提交与事务处理

Entity Framework中的批量提交与事务处理

 

在Entity Framework 中使用SaveChanges()是很频繁的,单次修改或删除数据后调用SaveChanges()返回影响记录数。

要使用批量修改或者批量删除数据,就需要SaveChanges(false)+AcceptAllChanges()方法了。

 SaveChanges(false) 只是通知EF需要对数据库执行的操作,在内存中是属于挂起状态,在必要的时候是可以撤销的,比如AcceptAllChange()提交为真正成功,EF将撤销SaveChanges(false)的操作。


 而在处理分布式事务操作的时候,就有必要使用TransactionScope 来处理了,很多时候我们会这样写:

using (TransactionScope scope = new TransactionScope())
{
   
//Do something with context1
   
//Do something with context2

   
//Save and discard changes
    context1
.SaveChanges();

   
//Save and discard changes
    context2
.SaveChanges();

   
//if we get here things are looking good.
    scope
.Complete();
}
但是这样写是有风险的,假 

context1.SaveChanges()成功了,context2.SaveChanges()却是有问题的,我们在scope.Complete()提交事务的时候就会终止,而Context1已经成功执行了

这可能不一定符合我们的需要。如果我们需要 context1、context2要不同时执行成功,要不都不成功,我们需要对代码作小小的调整,如用下面的代码: 

 

using (TransactionScope scope = new TransactionScope())
{
   
//Do something with context1
   
//Do something with context2

   
//Save Changes but don't discard yet
    context1
.SaveChanges(false);

   
//Save Changes but don't discard yet
    context2
.SaveChanges(false);

   
//if we get here things are looking good.
    scope
.Complete();
    context1
.AcceptAllChanges();
    context2
.AcceptAllChanges();

}
我们用SaveChanges(false)先将必要的数据库操作命令发送给数据库,这是注意context1与context2并没有真正发生改变,如果事务终止,自动回滚,两者的更改都没有真正提交到数据库,所以是可以成功回滚的。

 

 

        testModel.testEntities1 test = new testModel.testEntities1();        //test.Connection.Open();        //test.ExecuteStoreCommand("insert into userinfo (id,username,password) values(44554,'aaa','dddd')");        //System.Data.Common.DbTransaction trans = test.Connection.BeginTransaction();        // UserInfo user = new UserInfo { id = "111111111", password = "a", username = "a" };        UserInfo user1 = new UserInfo { id = "111144111", password = "ddd", username = "地对地导弹" };        Email em = new Email { emal = "dd", userid = "111111111", id = "dd" };        // test.AddObject("UserInfo", user);        test.AddObject("Email", em);        test.AddObject("UserInfo", user1);        test.SaveChanges().ToString();//对相同表,还是不相同表操作都是事务        //trans.Commit();        //testModel.testEntities1 test = new testModel.testEntities1();        //test.Connection.Open();        //test.ExecuteStoreCommand("insert into userinfo (id,username,password) values(44554,'aaa','dddd')");        //System.Data.Common.DbTransaction trans = test.Connection.BeginTransaction();        //UserInfo user = new UserInfo { id = "23333332", password = "ddd", username = "dddd" };        //test.AddObject("UserInfo", user);        //Response.Write(test.SaveChanges().ToString());        //trans.Commit();
Entity Framework中的批量提交与事务处理用发现的眼光来看这个互联网,总有我们立脚的地方!——北纬28.33
posted @ 2011-03-25 00:35 北纬28.33 阅读(1750) 评论(10) 编辑 收藏
        testModel.testEntities1 test = new testModel.testEntities1();        //test.Connection.Open();        //test.ExecuteStoreCommand("insert into userinfo (id,username,password) values(44554,'aaa','dddd')");        //System.Data.Common.DbTransaction trans = test.Connection.BeginTransaction();        // UserInfo user = new UserInfo { id = "111111111", password = "a", username = "a" };        UserInfo user1 = new UserInfo { id = "111144111", password = "ddd", username = "地对地导弹" };        Email em = new Email { emal = "dd", userid = "111111111", id = "dd" };        // test.AddObject("UserInfo", user);        test.AddObject("Email", em);        test.AddObject("UserInfo", user1);        test.SaveChanges().ToString();//对相同表,还是不相同表操作都是事务        //trans.Commit();        //testModel.testEntities1 test = new testModel.testEntities1();        //test.Connection.Open();        //test.ExecuteStoreCommand("insert into userinfo (id,username,password) values(44554,'aaa','dddd')");        //System.Data.Common.DbTransaction trans = test.Connection.BeginTransaction();        //UserInfo user = new UserInfo { id = "23333332", password = "ddd", username = "dddd" };        //test.AddObject("UserInfo", user);        //Response.Write(test.SaveChanges().ToString());        //trans.Commit();
Entity Framework中的批量提交与事务处理

不错。顶。。。
貌似EF的操作本身就是事务性的。。。
没看懂. 同2L,context本来就是事务性的.

为什么会有2个不同的context.
http://blogs.msdn.com/b/wriju/archive/2011/03/14/ado-net-entity-framework-transaction.aspx
@我是你的猪
没错,EF本身的操作是具有事务机制的,但是示例中有两个不同Context,他们属于分布式操作,已经脱离了EF的事务处理,所以这个时候TransactionScope就起到作用了
@要有好的心情
Good!
分布式操作是指? 只要在一个解决方案里,都能合并懂到一个context上啊.
@我是你的猪
TransactionScope可以对多次数据库连接保持事务一致性
.NET中常用的事务有Transaction,TransactionScope ,MSTDC...
http://msdn.microsoft.com/zh-cn/library/h5w5se33(v=VS.90).aspx#Y600
 

在Entity Framework 中使用SaveChanges()是很频繁的,单次修改或删除数据后调用SaveChanges()返回影响记录数。

要使用批量修改或者批量删除数据,就需要SaveChanges(false)+AcceptAllChanges()方法了。

 SaveChanges(false) 只是通知EF需要对数据库执行的操作,在内存中是属于挂起状态,在必要的时候是可以撤销的,比如AcceptAllChange()提交为真正成功,EF将撤销SaveChanges(false)的操作。


 而在处理分布式事务操作的时候,就有必要使用TransactionScope 来处理了,很多时候我们会这样写:

using (TransactionScope scope = new TransactionScope())
{
   
//Do something with context1
   
//Do something with context2

   
//Save and discard changes
    context1
.SaveChanges();

   
//Save and discard changes
    context2
.SaveChanges();

   
//if we get here things are looking good.
    scope
.Complete();
}
但是这样写是有风险的,假 

context1.SaveChanges()成功了,context2.SaveChanges()却是有问题的,我们在scope.Complete()提交事务的时候就会终止,而Context1已经成功执行了

这可能不一定符合我们的需要。如果我们需要 context1、context2要不同时执行成功,要不都不成功,我们需要对代码作小小的调整,如用下面的代码: 

 

using (TransactionScope scope = new TransactionScope())
{
   
//Do something with context1
   
//Do something with context2

   
//Save Changes but don't discard yet
    context1
.SaveChanges(false);

   
//Save Changes but don't discard yet
    context2
.SaveChanges(false);

   
//if we get here things are looking good.
    scope
.Complete();
    context1
.AcceptAllChanges();
    context2
.AcceptAllChanges();

}
我们用SaveChanges(false)先将必要的数据库操作命令发送给数据库,这是注意context1与context2并没有真正发生改变,如果事务终止,自动回滚,两者的更改都没有真正提交到数据库,所以是可以成功回滚的。

 

 

相关文章:

  • 2022-12-23
  • 2022-01-07
  • 2021-09-09
  • 2021-04-26
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
  • 2021-12-15
  • 2022-12-23
相关资源
相似解决方案