实体框架 - 使用事务或调用SaveChanges(假)和AcceptAllChanges()? [英] Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?

查看:194
本文介绍了实体框架 - 使用事务或调用SaveChanges(假)和AcceptAllChanges()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在调查的交易,看来,他们照顾自己的EF只要我通过的SaveChanges(),然后调用 AcceptAllChanges()如果没有错误:

I have been investigating transactions and it appears that they take care of themselves in EF as long as I pass false to SaveChanges() and then call AcceptAllChanges() if there are no errors:

SaveChanges(false);
// ...
AcceptAllChanges();

如果事情变坏呢?不用我必须尽快我的方法超出范围或回滚,是交易结束?

What if something goes bad? don't I have to rollback or, as soon as my method goes out of scope, is the transaction ended?

恰好那么通过交易分配一半的indentiy列是什么?我presume如果有人矿难发生后其他人增加了一个记录之前雷坏了那么这意味着会有一个丢失的标识值。

What happens to any indentiy columns that were assigned half way through the transaction? I presume if somebody else added a record after mine before mine went bad then this means there will be a missing Identity value.

是否有任何理由使用标准的的TransactionScope 在我的code类?

Is there any reason to use the standard TransactionScope class in my code?

推荐答案

通过实体框架的大部分时间的SaveChanges()就足够了。这就产生了一个交易,或在任何环境事务中登记,并执行该交易所有必要的工作。

With the Entity Framework most of the time SaveChanges() is sufficient. This creates a transaction, or enlists in any ambient transaction, and does all the necessary work in that transaction.

虽然有时在的SaveChanges(假)+ AcceptAllChanges()配对是非常有用的。

Sometimes though the SaveChanges(false) + AcceptAllChanges() pairing is useful.

造成这种情况的最有用的地方是要在两个不同的情况下做分布式事务的情况。

The most useful place for this is in situations where you want to do a distributed transaction across two different Contexts.

即。像这样(坏):

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()失败,整个分布式事务被中止。但不幸的是实体框架已丢弃 CONTEXT1 的变化,所以你不能重播或有效登录失败。

If context1.SaveChanges() succeeds but context2.SaveChanges() fails the whole distributed transaction is aborted. But unfortunately the Entity Framework has already discarded the changes on context1, so you can't replay or effectively log the failure.

但是,如果你改变你的code看起来是这样的:

But if you change your code to look like this:

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(假)发送给数据库必要的命令,上下文本身不改变,这样你就可以在必要时再次这样做,或者你可以询问 ObjectStateManager 如果你想。

While the call to SaveChanges(false) sends the necessary commands to the database, the context itself is not changed, so you can do it again if necessary, or you can interrogate the ObjectStateManager if you want.

这意味着,如果交易实际上中止您可以弥补,方法是重新尝试或记录每个上下文状态 ObjectStateManager 的某个地方。

This means if the transaction actually aborts you can compensate, by either re-trying or logging state of each contexts ObjectStateManager somewhere.

请参阅博客文章了解。

这篇关于实体框架 - 使用事务或调用SaveChanges(假)和AcceptAllChanges()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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