r3p*_*ica 3 c# entity-framework eager-loading
我有这个存储库:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个服务,看起来像这样:
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>();
}
}
Run Code Online (Sandbox Code Playgroud)
我的页面服务看起来像这样(简化):
public class PageService : Service<Page>
{
// ...
public IList<Page> GetPublished()
{
return this.Repository.GetAll(new string[] { "ForbiddenUsers", "ForbiddenGroups" }).Where(model => model.Published).ToList();
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
为了清楚起见,我的页面看起来像这样:
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; }
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,ForbiddenUsers和ForbiddenGroups始终为null.如果我检查调用堆栈,我可以看到生成的查询如下所示:
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]
Run Code Online (Sandbox Code Playgroud)
如你所见,这完全忽略了我的包含.
如果我将服务方法更改为:
public IList<Page> GetPublished()
{
return this.Repository.GetAll("ForbiddenUsers").Where(model => model.Published).ToList();
}
Run Code Online (Sandbox Code Playgroud)
现在运行我的代码,ForbiddenUsers现在已填充,调用堆栈显示正确生成的查询(太大而无法在此处粘贴).
我需要允许多个包含但我无法弄清楚他们为什么不工作....
任何帮助将不胜感激....
您应该将包含的查询分配回查询变量,因为QueryableExtensions.Include创建new DbQuery<T>而不是修改现有查询.另外我建议您使用params包含的路径:
public IEnumerable<T> GetAll(params string[] includes)
{
IQueryable<T> query = context.Set<T>();
foreach (var include in includes)
query = query.Include(include);
return query;
}
Run Code Online (Sandbox Code Playgroud)
这将允许您传递所有包含的路径,而无需显式创建数组:
public IList<Page> GetPublished()
{
return Repository.GetAll("ForbiddenUsers", "ForbiddenGroups")
.Where(model => model.Published)
.ToList();
}
Run Code Online (Sandbox Code Playgroud)