ObjectContext.AcceptAllChanges!谁在接受谁和正在进行更改? [英] ObjectContext.AcceptAllChanges! Who is accepting and who is making the changes?

查看:72
本文介绍了ObjectContext.AcceptAllChanges!谁在接受谁和正在进行更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被Entity Framework ObjectContext.AcceptAllChanges所困惑。问题是,谁在接受谁进行更改?



一般来说,如何控制Entity Framework中的事务和事务隔离?



我是一个新用户,所以我只能编辑自己的问题.......



谢谢你们... Akram Shahda和Ladislav Mrnka



现在我对EF的印象已经不是很糟糕了,虽然它与旧时代相比没有增加价值功能Connection,Transaction ,ADO.Net,存储过程样式数据库访问,除了可能会添加一些潜在的安全问题,并向DBA添加一些管理工作(因为连接字符串需要具有所有表的权限),并限制使用某些性能我很高兴我仍然可以通过Enlist(另一个微软)保留对事务和事务隔离级别的相同级别的控制。新词,为什么呢不是他们只是使用使用)我管理自己的连接和TransactionScope,我可以选择我想要的隔离级别。



现在,我的问题有关ObjectContext。 AcceptAllChanges是:


  1. 谁在进行更改?

  2. 谁接受更改? / li>

谢谢......

解决方案

默认情况下 AcceptAllChanges SaveChanges 期间调用。如果保存成功,则接受更改。您可以通过调用 AcceptAllChanges 手动关闭此操作并接受更改。这在一些更高级的场景中可能是有用的。其中一种情况可能是处理多个事务资源的复杂事务。



例如,我们假设我要将数据保存到数据库,并将消息发送到MSMQ分布式事务:

  using(var scope = new TransactionScope())
{
//此版本将不会自动接受更改
context.SaveChanges(SaveOptions.DetectChangesBeforeSave);

//我们假设这个消息发送到消息队列
myMessageSender.SendSomeMessage(...);

scope.Complete();

//现在我知道事务完成了,我可以接受
//上下文中的更改
context.AcceptAllChanges();
}

如果我在示例中没有使用手动接受发送消息中的错误将回滚事务(所以数据不会被保留到数据库),但是我的上下文将处于所有更改都被接受的状态,我将无法再处理它们。


I am confused by the Entity Framework ObjectContext.AcceptAllChanges. The question is, Who is accepting and who is making the changes?

In general, how to control transactions and transaction isolation in Entity Framework?

I am a new user, so I can only edit my own question .......

Thank you guys ... Akram Shahda and Ladislav Mrnka

Now my impression with EF is not very bad anymore, although it adds no value functionality wise compared with the good-old-days "Connection, Transaction, ADO.Net, Stored procedure" style database access, except may be adding some potential security problems and add some administrative works to the DBAs (as the connection string needs to have the permission to all the tables) and limit the use of some performance improvement techniques particularly during some batch database operations.

I am happy that I can still retain the same level of control on transactions and transaction isolation level by "Enlist" (another Microsoft new word, why do not they just use "use") my managing my own connection and "TransactionScope" where I can choose my desired isolation level.

Now, my question about the "ObjectContext.AcceptAllChanges" are:

  1. Who is making the changes?
  2. Who is accepting the changes?

Thanks ......

解决方案

By default AcceptAllChanges is called during SaveChanges. If saving succeeds changes are accepted. You can turn this off and accept changes manually by calling AcceptAllChanges. This can be useful in some more advanced scenarios. One of such scenarios can be complex transaction where multiple transactional resources are handled.

For example let's assume that I want to save data to database and send message to MSMQ in the same distributed transaction:

using (var scope = new TransactionScope())
{
    // This version will not accept changes automatically
    context.SaveChanges(SaveOptions.DetectChangesBeforeSave);

    // Let's assume this sends message to the message queue
    myMessageSender.SendSomeMessage(...);

    scope.Complete();

    // Now I know that transaction is completed and I can accept
    // changes in the context
    context.AcceptAllChanges();
}

If I didn't use manual accepting in the example the error in sending message would rolled back the transaction (so data would not be persisted to the database) but my context would be in state where all changes are accepted and I would not be able to process them again.

这篇关于ObjectContext.AcceptAllChanges!谁在接受谁和正在进行更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆