gpt4 book ai didi

entity-framework-5 - 使用 Entity Framework Code First 进行事务处理

转载 作者:行者123 更新时间:2023-12-04 07:54:57 25 4
gpt4 key购买 nike

有答案here解释如何使用 Entity Framework 进行交易
但是,该解决方案不适用于代码。
我已经尝试过,我的测试表明以下确实有效

using (var scope = new TransactionScope())
{
DBContext1.SaveChanges()
If (ForceFailure) return 0 // used in testing
DBContext2.SaveChanges()
scope.Complete();
}

但是我感到紧张,因为我不再将参数传递给 SaveChanges 或调用 AcceptAllChanges

我如何确定我是否可以信任我的解决方案?

最佳答案

SaveChanges(Boolean)ObjectContext 的一部分,不是 DbContext .注意方法SaveChanges(Boolean)ObjectContext不赞成使用 SaveChanges(SaveOptions) .

如果您还想使用 DbContext ,你也许可以重载它自己的 SaveChanges使用方法IObjectContextAdapter.ObjectContext ,例如通过使用扩展方法。

关于entity-framework-5 - 使用 Entity Framework Code First 进行事务处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14944802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com