Swaff
Swaff

Reputation: 13621

How can I get Entity Framework 1.0 to use the nolock hint when accessing a specific entity

I would like to access an entity (called Document) which is mapped to a table (called Document) using Entity Framework 1.0 but it is very important that this table is not locked when the data is read.

There are articles both online, and on stackoverflow that suggest using transaction scope for this. But I don't initially feel comfortable creating a transaction for a read.

Therefore my question is:

  1. How can I force entity framework to use the nolock hint when creating the SQL statement for this entity, without pointing at a stored procedure, when the query is directed at SQL Server 2008?
  2. If this is not possible are there any real issues with using a transaction scope for a read, or am I just being too cautious?

Upvotes: 3

Views: 455

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364319

You can't force EF to use NOLOCK hint until you write your custom entity framework provider where you will control creation of all SQL statements. If you want to try it check these provider wrappers which can be used to wrap existing EF providers. But still it would require parsing and modifying already created SQL statements which can be quite complex task with big impact on performance.

As I understand it, each statement runs in its own transaction. That is the reason why locking causes issues. Wrapping statements in TransactionScope with lower isolation level just reconfigures implicit transaction. But be aware that uncommited reads are also called dirty reads - you can get uncommited data.

Upvotes: 2

Related Questions