我已關閉延遲加載和我的DbContext上的代理創建。我正在使用Repository partern和UnitOfWork。我的UnitOfWork繼承自DBConext。下面是我在做什麼的例子:調用DbContext.Set <T>().Include()不查詢包含的屬性?
public class User
{
public Guid Id {get;set;}
public virtual Guid UserTypeId {get;set;} //foreign key to UserType and setup in the EF fluent mappings and it does load if I am using lazy loading.
public virtual UserType {get;set;}
}
public class UserType
{
public Guid Id {get;set;}
public string Name {get;set;}
}
這是我的UOW內:
public IDbSet<TEntity> CreateSet<TEntity>() where TEntity : class
{
return base.Set<TEntity>();
}
我查詢通過我的資源庫上下文:
protected Expression<Func<TEntity, object>>[] Includes;
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> criteria, params Expression<Func<TEntity, object>>[] includes)
{
Includes = includes;
return GetSet().Where(criteria)
.AsEnumerable();
}
public IDbSet<TEntity> GetSet()
{
var set = _unitOfWork.CreateSet<TEntity>();
if(Includes != null)
{
foreach (var include in Includes)
{
set.Include(include);
}
}
return set;
}
所以,你可以看到我傳遞了一個包含在我的查詢中的表達式數組。所以我可能會這樣稱呼它:
var users = userRespository.Get(u => u.Id == SomeGuid, u => u.UserType);
UserType未包含在查詢中,我不知道是什麼。我應該在DbContext上調用Set以外的其他東西嗎?
更新:
我想我之前所說的base.Set我將需要添加包含有。但不知道。
如果使用:set = set.Include(include);'? – 2012-03-23 14:04:33
@LadislavMrnka,從來沒有想過......我會試試看...... – DDiVita 2012-03-23 14:06:19
@LadislavMrnka,如果你想讓答案,我會給你信貸。這工作完美! – DDiVita 2012-03-23 14:11:29