如果我的生活依赖于它,我无法让QueryObjectGraph添加INCLUDE子表...我错过了什么?坚持第三天应该简单的事情: - /
DAL:
public abstract class RepositoryBase<T> where T : class
{
private MyLPL2Context dataContext;
private readonly IDbSet<T> dbset;
protected RepositoryBase(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
dbset = DataContext.Set<T>();
DataContext.Configuration.LazyLoadingEnabled = true;
}
protected IDatabaseFactory DatabaseFactory
{
get;
private set;
}
protected MyLPL2Context DataContext
{
get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}
public IQueryable<T> QueryObjectGraph(Expression<Func<T, bool>> filter,
params string[] children)
{
foreach (var child in children)
{
dbset.Include(child);
}
return dbset.Where(filter);
}
... DAL存储库
public interface IBreed_TranslatedSqlRepository : ISqlRepository<Breed_Translated>
{
}
public class Breed_TranslatedSqlRepository : RepositoryBase<Breed_Translated>,
IBreed_TranslatedSqlRepository
{
public Breed_TranslatedSqlRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{}
}
BLL回购:
public IQueryable<Breed_Translated>
QueryObjectGraph(Expression<Func<Breed_Translated, bool>> filter,
params string[] children)
{
return _r.QueryObjectGraph(filter, children);
}
控制器:
var breeds1 = _breedTranslatedRepository
.QueryObjectGraph(b => b.Culture == culture, new string[] { "AnimalType_Breed" })
.ToList();
我无法访问Breed.AnimalType_Breed.AnimalTypeId
..我可以钻到Breed.AnimalType_Breed,那么intelisense需要一个表达式吗?
如果有的话,DB表:粗体是多对多的 品种,Breed_Translated, AnimalType_Breed ,AnimalType,......
答案 0 :(得分:0)
如果你看看包含它有一个返回类型。 Linq主要是功能性的,所以它不会改变任何对象,而是返回新的对象。您需要存储此新对象。
尝试:
var query = dbset.Where(filter);
foreach (var child in children)
{
query = query.Include(child);
}
return query;
其他注意:您可以简化:
var breeds1 = _breedTranslatedRepository
.QueryObjectGraph(b => b.Culture == culture, new string[] { "AnimalType_Breed" })
.ToList();
到
var breeds1 = _breedTranslatedRepository
.QueryObjectGraph(b => b.Culture == culture, "AnimalType_Breed")
.ToList();
当你的方法采用params string []参数
时答案 1 :(得分:0)
AnimalBreed_Type
表示多对多关系,因此AnimalBreed_Type
实体中的Breed_Translated
属性为集合!集合类型没有表属性。您必须使用First
或Single
从此集合中获取单个相关实体,并将其检查为AnimalTypeId
。