Writing an extension method for Entity Framework
问题 I would like to have an extension method called FirstOrDefaultCache() which would check dbContext.EntityName.Local.FirstOrDefault(condition) , and only if that is null, check dbContext.EntityName.FirstOrDefault(condition) . I got the following from another post, which works okay: public static TEntity FirstOrDefaultCache<TEntity>(this DbSet<TEntity> queryable, Expression<Func<TEntity, bool>> condition) where TEntity : class { return queryable .Local.FirstOrDefault(condition.Compile()) // find