1
对于第一个,我试图避免在我的程序集中直接链接到EntityFramework
,所以我不能在客户端代码中使用System.Data.Entity
命名空间,只能在接口实现类中使用。使用Include(string)方法包含几个导航属性
我有接口
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
集合只有最新的导航属性已初始化, G。 SystemUser
。
一些代码的修改,就像这样:
public IQueryable<T> WithProperties(params string[] properties)
{
foreach (var property in properties)
{
this.DbSet.Include(property);
}
return this.DbSet;
}
任何结果
笑,这是显而易见的。谢谢! – dotFive