Entity framework 4.0 and 5.0 Code First IQueryable query with NOLOCK
I am using EF 5.0 Code First. We cannot implement nolock for every read request. Below is the code.
My model:
public class UserType
{
public UserType()
{
CreationDate = DateTime.Now;
UpdatedDate = DateTime.Now;
DeletedFlag = false;
}
public int UserTypeId { get; set; }
public string UserTypeName { get; set; }
public int CreatedBy { get; set; }
public int UpdatedBy { get; set; }
public DateTime CreationDate { get; set; }
public DateTime UpdatedDate { get; set; }
public bool DeletedFlag { get; set; }
}
RepositoryBase: (we are using the repository design pattern)
public IQueryable<T> Query<T>() where T : class
{
return DataContext.Set<T>();
}
Services:
//TODO: place NOLOCK
repBase.Query<UserType>().Where(ja => ja.DeletedFlag == false).OrderByDescending(ja => ja.UpdatedDate).ToList();
repBase is the DB context instance to call above Query ().
Now we want to execute the query above (NOLOCK).
Visual Studio 2012, EF 5.0 Code First, C # .Net 4.0, MVC Web API, Repository Design Pattern
Thanks in advance.
source to share
Not supported. EF never emits NOLOCK
in queries and there is no way to do it other than executing direct SQL through Database.SqlQuery
or DbSet.SqlQuery
. If you want to have an NOLOCK
EF generated, you can download the EF source code and change its SQL generation engine to add a hint. In any case, you should think twice about NOLOCK
worldwide use.
source to share