我明白,EF 5会自动缓存查询,但是它是根据上下文还是整体来做?我们正在使用MVC,并且使用block封装调用以处理dbcontext.例如:
public class Employee { public string FirstName {get; set;} public string LastName {get; set;} public int ID {get; set;} } public class EmployeeQueryByFirstName : IQueryObject<Employee> { private string _firstName; public void Set(string FirstName) { _firstName = FirstName; } public Expression<Func<Employee,bool>> AsExpression() { return (e=>e.FirstName == this._firstName); } } public class RepoExcerpt { public TEntity Find<TEntity>(IQueryObject<TEntity> queryObject) where TEntity : class { using (var conn = ServiceLocator.IOC.Resolve<IDbContext>()) { var query = (from q in conn.Set<TEntity>() select q); query = query.Where(queryObject.AsExpression()); return query.FirstOrDefault(); } } }
下一次我们在存储库中调用Find,EF 5会有这个查询的缓存版本,还是会消失,因为我们将得到一个新的dbcontext?如果我想要缓存查询,我需要处理吗?