使用 Include(string) 方法包含多个导航属性
Include several navigation properties with Include(string) method
首先,我试图避免在我的程序集中直接 link 到 EntityFramework
,所以我不能在客户端代码中使用 System.Data.Entity
命名空间,只能在接口中使用实施 class.
我有接口
public interface IEntitySource<T>
where T : class
{
...
IQueryable<T> WithProperties(params string[] properties);
...
}
它是 EF 实现:
public class EfEntitySource<T> : IEntitySource<T>
where T : class
{
public IQueryable<T> WithProperties(params string[] properties)
{
IQueryable<T> queryable = this.DbSet;
foreach (var property in properties)
{
queryable = this.DbSet.Include(property);
}
return queryable;
}
}
和客户端代码:
public IEnumerable<ContentChangeHistory> GetContentActivityAtGroup(Guid groupId)
{
var groupActivity = this.ContentChangeHistorySource
.WithProperties("ContentPost", "ContentItem", "SystemUser")
.Where(cch => cch.ChangeGroupId == groupId);
return groupActivity;
}
但是,执行 GetContentActivityAtGroup
方法的代码 returns ContentChangeHistory
仅初始化最新导航 属性 的集合,例如SystemUser
.
一些代码修改,像这样:
public IQueryable<T> WithProperties(params string[] properties)
{
foreach (var property in properties)
{
this.DbSet.Include(property);
}
return this.DbSet;
}
没有结果
改变
queryable = this.DbSet.Include(property);
至
queryable = queryable.Include(property);
首先,我试图避免在我的程序集中直接 link 到 EntityFramework
,所以我不能在客户端代码中使用 System.Data.Entity
命名空间,只能在接口中使用实施 class.
我有接口
public interface IEntitySource<T>
where T : class
{
...
IQueryable<T> WithProperties(params string[] properties);
...
}
它是 EF 实现:
public class EfEntitySource<T> : IEntitySource<T>
where T : class
{
public IQueryable<T> WithProperties(params string[] properties)
{
IQueryable<T> queryable = this.DbSet;
foreach (var property in properties)
{
queryable = this.DbSet.Include(property);
}
return queryable;
}
}
和客户端代码:
public IEnumerable<ContentChangeHistory> GetContentActivityAtGroup(Guid groupId)
{
var groupActivity = this.ContentChangeHistorySource
.WithProperties("ContentPost", "ContentItem", "SystemUser")
.Where(cch => cch.ChangeGroupId == groupId);
return groupActivity;
}
但是,执行 GetContentActivityAtGroup
方法的代码 returns ContentChangeHistory
仅初始化最新导航 属性 的集合,例如SystemUser
.
一些代码修改,像这样:
public IQueryable<T> WithProperties(params string[] properties)
{
foreach (var property in properties)
{
this.DbSet.Include(property);
}
return this.DbSet;
}
没有结果
改变
queryable = this.DbSet.Include(property);
至
queryable = queryable.Include(property);