【发布时间】:2014-02-05 10:17:58
【问题描述】:
我有这个存储库:
public class Repository<T> : IRepository<T> where T : class
{
private readonly DbContext context;
private readonly DbSet<T> dbEntitySet;
public Repository(DbContext context)
{
if (context == null)
throw new ArgumentNullException("context");
this.context = context;
this.dbEntitySet = context.Set<T>();
}
public IEnumerable<T> GetAll()
{
return this.dbEntitySet;
}
public IEnumerable<T> GetAll(string include)
{
return this.dbEntitySet.Include(include);
}
public IEnumerable<T> GetAll(string[] includes)
{
IQueryable<T> query = context.Set<T>();
foreach (var include in includes)
query.Include(include);
return query;
}
public void Create(T model)
{
this.dbEntitySet.Add(model);
}
public void Update(T model)
{
this.context.Entry<T>(model).State = EntityState.Modified;
}
public void Remove(T model)
{
this.context.Entry<T>(model).State = EntityState.Deleted;
}
public void Dispose()
{
this.context.Dispose();
}
}
我有一个如下所示的服务:
public class Service<T> where T : class
{
private readonly IRepository<T> repository;
protected IRepository<T> Repository
{
get { return this.repository; }
}
internal Service(IUnitOfWork unitOfWork)
{
this.repository = unitOfWork.GetRepository<T>();
}
}
我的页面服务如下所示(简化):
public class PageService : Service<Page>
{
// ...
public IList<Page> GetPublished()
{
return this.Repository.GetAll(new string[] { "ForbiddenUsers", "ForbiddenGroups" }).Where(model => model.Published).ToList();
}
// ...
}
为了清楚起见,我的页面如下所示:
public enum PageType
{
Root,
System,
Page,
Link,
Group
}
public partial class Page
{
public int Id { get; set; }
public System.DateTime DateCreated { get; set; }
public string CreatedById { get; set; }
public Nullable<System.DateTime> DateModified { get; set; }
public string ModifiedById { get; set; }
public string CompanyId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string FileName { get; set; }
public string Path { get; set; }
public string ViewData { get; set; }
public string ViewTitle { get; set; }
public string Link { get; set; }
public bool Published { get; set; }
public PageType Type { get; set; }
public int Order { get; set; }
public string Lineage { get; set; }
public Nullable<int> ParentId { get; set; }
public bool Restricted { get; set; }
public bool Deleted { get; set; }
public Company Company { get; set; }
public User CreatedBy { get; set; }
public User ModifiedBy { get; set; }
public Page Parent { get; set; }
public MenuPage MenuPage { get; set; }
public ICollection<Page> Pages { get; set; }
public ICollection<User> ForbiddenUsers { get; set; }
public ICollection<Group> ForbiddenGroups { get; set; }
}
当我运行此代码时,ForbiddenUsers 和 ForbiddenGroups 始终为空。 如果我检查调用堆栈,我可以看到生成的查询如下所示:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[DateCreated] AS [DateCreated],
[Extent1].[CreatedById] AS [CreatedById],
[Extent1].[DateModified] AS [DateModified],
[Extent1].[ModifiedById] AS [ModifiedById],
[Extent1].[CompanyId] AS [CompanyId],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[FileName] AS [FileName],
[Extent1].[Path] AS [Path],
[Extent1].[ViewData] AS [ViewData],
[Extent1].[ViewTitle] AS [ViewTitle],
[Extent1].[Link] AS [Link],
[Extent1].[Published] AS [Published],
[Extent1].[Type] AS [Type],
[Extent1].[Order] AS [Order],
[Extent1].[Lineage] AS [Lineage],
[Extent1].[ParentId] AS [ParentId],
[Extent1].[Restricted] AS [Restricted],
[Extent1].[Deleted] AS [Deleted]
FROM [dbo].[Pages] AS [Extent1]
如您所见,这完全忽略了我的包含。
如果我将服务方法更改为:
public IList<Page> GetPublished()
{
return this.Repository.GetAll("ForbiddenUsers").Where(model => model.Published).ToList();
}
运行我的代码,ForbiddenUsers 现在已填充,调用堆栈显示正确生成的查询(太大,无法粘贴到此处)。
我需要允许多个包含,但我不知道为什么它们不起作用....
任何帮助将不胜感激......
【问题讨论】:
标签: c# entity-framework eager-loading