Database 实体框架事务锁定数据库
Db隔离级别设置为Database 实体框架事务锁定数据库,database,entity-framework,Database,Entity Framework,Db隔离级别设置为ReadCommitted 我用了: (var trans = _dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted)) 但这仍然锁定了数据库中的表 在运行另一组执行之前,我需要这些值,但在所有操作完成之前,我不想将这些值提交到DB中 using (var trans = _dbContext.Database.BeginTransaction(System.Data.
ReadCommitted
我用了:
(var trans = _dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
但这仍然锁定了数据库中的表
在运行另一组执行之前,我需要这些值,但在所有操作完成之前,我不想将这些值提交到DB中
using (var trans = _dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
{
int value=getNewValue(oldvalue);
int someothervalue=DoSomeOtherAction(value);
//Some more methods same like this
trans.Commit();
return true;
}
catch
(Exception e)
{
trans.Rollback();
_logger.Error(e);
throw e;
return false;
}
// Here is the GetNewValue Method
Public int getNewValue(int oldvalue)
{
var job = _dbContext.VALUE_DATA.FirstorDefault(x => x.value == oldvalue);
var someData = new VALUE_DATA();
someData.status = job.status;
someData.Date = job.Date;
someData.Source = job.Source;
someData.Start_DT = job.Start_DT;
someData.End_DT = job.End_DT;
someData.Notes = job.Notes;
_dbContext.Set <VALUE_DATA >().Add(someData);
_dbContext.SaveChanges();
return someData.ID;
}
// DoSomeOtherAction Method
Public int DoSomeOtherAction(int value)
{
var job = _dbContext.SOME_TABLE.FirstorDefault(x => x.value == value);
var someValue = new SOME_TABLE();
someValue.Name=job.Name;
someValue.Address=job.Address;
_dbContext.Set <SOME_TABLE >().Add(someValue);
_dbContext.SaveChanges();
Return someValue.ID;
}
使用(var trans=_dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
{
int value=getNewValue(oldvalue);
int someothervalue=DoSomeOtherAction(值);
//还有一些像这样的方法吗
trans.Commit();
返回true;
}
抓住
(例外情况e)
{
trans.Rollback();
_错误(e);
投掷e;
返回false;
}
//下面是GetNewValue方法
公共整数getNewValue(整数oldvalue)
{
var job=\u dbContext.VALUE\u DATA.FirstorDefault(x=>x.VALUE==oldvalue);
var someData=新值_DATA();
someData.status=job.status;
someData.Date=job.Date;
someData.Source=job.Source;
someData.Start\u DT=job.Start\u DT;
someData.End_DT=job.End_DT;
someData.Notes=job.Notes;
_dbContext.Set().Add(someData);
_dbContext.SaveChanges();
返回someData.ID;
}
//剂量热作用法
公共int DoSomeOtherAction(int值)
{
var job=\u dbContext.SOME\u TABLE.FirstorDefault(x=>x.value==value);
var someValue=新的SOME_表();
someValue.Name=job.Name;
someValue.Address=job.Address;
_dbContext.Set().Add(someValue);
_dbContext.SaveChanges();
返回someValue.ID;
}
只需使用基本方法解决问题:
var transactionOptions = new System.Transactions.TransactionOptions();
// change the leavel to Read Uncommited
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
// Use the TransactionScope!
using (var transactionScope = new System.Transactions.TransactionScope(
System.Transactions.TransactionScopeOption.Required,
transactionOptions)
)
using (var _dbContext= new MyDbContext())
{
// Do your work here!
transactionScope.Complete();
}
如果您有其他线程正在更改使用的表,那么您可能会成为脏读问题
或者,您可以使用DbCommandInterceptor对Sql查询进行and“NOCHECK”操作。您可以提供有关您的问题的更多详细信息吗?您正在为一个事务而不是整个数据库设置READUNCOMMITED?!是的,我正在尝试仅为特定事务设置READUNCOMMIT,而不是为整个数据库设置READUNCOMMIT。是否可能?请以更好的方式解释问题,否则此问题将在没有任何答案的情况下关闭!我可以帮助你,这就是我要做的。在运行另一组执行之前,我需要这些值,但在所有操作完成之前,我不想将这些值提交到数据库中。坏消息是,除了threaqd或其他转换之外,您的数据库被锁定了吗@穆图安纳马拉伊,但它应该是有效的!如果一切都很干净,感谢您的跟进,实际上没有其他线程正在运行以锁定表…当我提取其他数据时,它没有锁定db。当我试图对事务内部的数据执行select时,从未提交的数据被锁定。但是当我使用select(nolock)时,它起了作用。^^^哦,问题终于找到并解决了,听到这个消息我很高兴