gpt4 book ai didi

c# - EF 预加载包括多个

转载 作者:行者123 更新时间:2023-11-30 13:46:27 25 4
gpt4 key购买 nike

我有这个存储库:

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; }
}

当我运行此代码时,ForbiddenUsersForbiddenGroups 始终为空。如果我检查调用堆栈,我可以看到生成的查询如下所示:

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 并且调用堆栈显示了正确生成的查询(太大而无法粘贴到此处)。

我需要允许多个包含,但我不明白为什么它们不起作用....

任何帮助将不胜感激....

最佳答案

您应该将包含的查询分配回您的查询变量,因为 QueryableExtensions.Include创建新的 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;
}

这将允许您在不显式创建数组的情况下传递所有包含的路径:

public IList<Page> GetPublished()
{
return Repository.GetAll("ForbiddenUsers", "ForbiddenGroups")
.Where(model => model.Published)
.ToList();
}

关于c# - EF 预加载包括多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21162955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com