实体框架和事务隔离级别

时间:2022-03-14 06:48:07

I'm using Entity Framework 4.0. Now I need to restrict access to a table while I'm reading from it or writing to it. Probably that's about transaction isolation level.

我正在使用Entity Framework 4.0。现在我需要限制对表的访问,当我从中读取或写入它时。可能是关于事务隔离级别的。

How do I do that?

我怎么做?

Update

更新

here is what I have

这就是我所拥有的

using (var db = new MyDb())
{
    using (TransactionScope scope = new TransactionScope())
    {
        var item = db.MyItems.Single(x => x.Id == 5);
        item.Price = 12;
        db.SaveChanges();
        scope.Complete(); 
    }
}

However, when I put a breakpoint at any line inside using (TransactionScope scope and when I'm stopping there and then I go to Sql Server Management Studio and doing a select query (or even update!) from a table that is using inside a transaction, I'm not getting an error for some reason. But why? It must not allow me to read a data while a transaction is executing.

但是,当我在内部任何一行使用断点时(TransactionScope作用域,当我停在那里然后我转到Sql Server Management Studio并从一个在内部使用的表中进行选择查询(甚至更新!))事务,我出于某种原因没有收到错误。但为什么呢?它不能让我在执行事务时读取数据。

1 个解决方案

#1


26  

By default a Transaction has an IsolationLevel of Serializable. Serializable is the highest level. It requires that the transaction completes before any other transaction is allowed to operate on the data.

默认情况下,Transaction具有Seri​​alizable的IsolationLevel。 Serializable是*别。它要求在允许任何其他事务对数据进行操作之前完成事务。

It has the following restrictions:

它有以下限制:

  • Statements cannot read data that has been modified but not yet committed by other transactions.
  • 语句无法读取已修改但尚未由其他事务提交的数据。
  • No other transactions can modify data that has been read by the current transaction until the current transaction completes.
  • 在当前事务完成之前,没有其他事务可以修改当前事务已读取的数据。
  • Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until the current transaction completes.
  • 其他事务无法插入具有键值的新行,这些键值将落在当前事务中任何语句读取的键范围内,直到当前事务完成为止。

This is a great blog post that explains how to use Transactions with the Entity Framework: Entity Framework transaction scope examples

这是一篇很棒的博客文章,解释了如何将事务与实体框架一起使用:实体框架事务范围示例

UPDATE

In Entity Framework 6 the default IsolationLevel is changed to READ_COMMITTED_SNAPSHOT for databases created using Code First, potentially allowing for more scalability and fewer deadlocks.See the future spec of EF 6 on codeplex

在Entity Framework 6中,对于使用Code First创建的数据库,默认的IsolationLevel更改为READ_COMMITTED_SNAPSHOT,可能允许更多的可伸缩性和更少的死锁。在codeplex上查看EF 6的未来规范

#1


26  

By default a Transaction has an IsolationLevel of Serializable. Serializable is the highest level. It requires that the transaction completes before any other transaction is allowed to operate on the data.

默认情况下,Transaction具有Seri​​alizable的IsolationLevel。 Serializable是*别。它要求在允许任何其他事务对数据进行操作之前完成事务。

It has the following restrictions:

它有以下限制:

  • Statements cannot read data that has been modified but not yet committed by other transactions.
  • 语句无法读取已修改但尚未由其他事务提交的数据。
  • No other transactions can modify data that has been read by the current transaction until the current transaction completes.
  • 在当前事务完成之前,没有其他事务可以修改当前事务已读取的数据。
  • Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until the current transaction completes.
  • 其他事务无法插入具有键值的新行,这些键值将落在当前事务中任何语句读取的键范围内,直到当前事务完成为止。

This is a great blog post that explains how to use Transactions with the Entity Framework: Entity Framework transaction scope examples

这是一篇很棒的博客文章,解释了如何将事务与实体框架一起使用:实体框架事务范围示例

UPDATE

In Entity Framework 6 the default IsolationLevel is changed to READ_COMMITTED_SNAPSHOT for databases created using Code First, potentially allowing for more scalability and fewer deadlocks.See the future spec of EF 6 on codeplex

在Entity Framework 6中,对于使用Code First创建的数据库,默认的IsolationLevel更改为READ_COMMITTED_SNAPSHOT,可能允许更多的可伸缩性和更少的死锁。在codeplex上查看EF 6的未来规范