gpt4 book ai didi

c# - Entity Framework 中的 NoLock

转载 作者:行者123 更新时间:2023-11-30 12:24:48 35 4
gpt4 key购买 nike

即使表由于特定事务而被锁定,我仍试图从表中读取记录。

我正在使用下面的代码

public async Task<KeyValuePair<String, List<Om_Category>>> CategoryList(Om_Category obj)
{
try
{
using (var transaction = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadUncommitted
},
TransactionScopeAsyncFlowOption.Enabled))
{
using (var categoryContext = new ModelGeneration())
{

categoryContext.Configuration.ProxyCreationEnabled = false;
var data = await categoryContext
.tblCategory
.ToListAsync();

transaction.Complete();

return new KeyValuePair<String, List<Om_Category>>("", data);
}
}
}
catch (Exception ex)
{
return new KeyValuePair<String, List<Om_Category>>(ex.Message, null);
}
}

But seems like I am missing something to implement NoLocks. Still it show timeout. Am I missing something ?

最佳答案

令人惊讶的是, Entity Framework 内置的事务类有效!!

public async Task<KeyValuePair<String, List<BE_Category>>> CategoryList(BE_Category obj)
{
try
{
using (var categoryContext = new ModelGeneration())
{
using (var dbContextTransaction = categoryContext
.Database
.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
{
categoryContext.Configuration.ProxyCreationEnabled = false;

//Code

dbContextTransaction.Commit();

return new KeyValuePair<String, List<BE_Category>>("", data);
}
}
}
catch (Exception ex)
{
return new KeyValuePair<String, List<BE_Category>>(ex.Message, null);
}
}

Reference

关于c# - Entity Framework 中的 NoLock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32858415/

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