gpt4 book ai didi

c# - Entity Framework : TransactionScope has a different IsolationLevel

转载 作者:行者123 更新时间:2023-11-30 16:05:23 24 4
gpt4 key购买 nike

我正在尝试在域级别使用 TransactionScope,因此我可以跨(可能)多个存储库操作数据,但将所有这些保存在同一事务中。

我有以下保存方法:

public void Save(MyEntity source)
{
using (var scope = new TransactionScope())
{
var context = new MyEFEntities(environment.ConnectionString);

this.Repository.Add(source.ToDbMyEntity(), context);

context.SaveChanges();

scope.Complete();
}
}

但我在 .SaveChanges() 上收到以下错误:

The transaction specified for TransactionScope has a different IsolationLevel than the value requested for the scope. Parameter name: transactionOptions.IsolationLevel

这是什么原因造成的?

最佳答案

我认为 Entity Framework 的默认隔离级别是数据库的默认隔离级别,例如 SqlServer 的默认隔离级别是 READ COMMITTED,而 TransactionScope 默认是 Serializable 因此,当您尝试在 using (var scope = new TransactionScope()) block 中更改它时,它会抛出错误。尝试这样的事情:

var transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions));

关于c# - Entity Framework : TransactionScope has a different IsolationLevel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673852/

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