我正在使用 EF 4.0,并且我想使用隔离级别serializable
,因为在事务中我想在读取时阻止寄存器。
好吧,在 SQL Server 中,我尝试使用以下命令更改隔离级别:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
在 C# 中,我使用此代码尝试阻止寄存器:
using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable }))
{
Entities myContext = new Entities();
string colors = "select * from Colors where IDColor = 2";
Colors myColor = myContext.Colors.SqlQuery(colors).FirstOrDefault<Colors>();
myColor.Color = "Red";
myContext.SaveChanges();
scope.Complete();
}
如果我逐行执行我的代码,如果我得到SaveChanges
但仍然没有执行,如果我使用 SQL Server Management Studio 对表格颜色进行查询,我可以获得记录。
然后,如果我执行保存更改并得到scope.Clompete()
,如果我尝试使用 Management Studio 进行查询,我什么也得不到,因为寄存器被阻止,所以当我完成我的 C# 代码时,寄存器被释放,我得到结果管理工作室。
所以我的问题是,如何在 C# 中使用可序列化的隔离级别?难道我做错了什么?
PS:我注意到如果我使用这个命令:
DBCC useroptions;
我看到隔离级别是serializable
,但是如果我关闭 Management Studio 并再次连接并看到隔离级别是默认的readCommitted
。
所以我的问题是如何为我的数据库设置默认隔离级别。