【发布时间】:2015-10-16 18:12:23
【问题描述】:
首先,我试图避免在我的程序集中直接链接到EntityFramework,所以我不能在客户端代码中使用System.Data.Entity命名空间,只能在接口实现类中使用。
我有界面
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 方法的代码返回ContentChangeHistory 集合,其中仅初始化了最新的导航属性,例如SystemUser.
一些代码修改,像这样:
public IQueryable<T> WithProperties(params string[] properties)
{
foreach (var property in properties)
{
this.DbSet.Include(property);
}
return this.DbSet;
}
没有结果
【问题讨论】:
标签: c# entity-framework iqueryable navigation-properties