如何急切地将 2 层包装的项目加载到集合中 (entity framework C#)
how to eagerly load 2 layers wrapped items into a collection (entity framework C#)
正在使用 Entity Framework 开发 WPF 应用程序。
我的实体 类 如下:
public partial class Pan
{
...
public virtual ICollection<Element> Elements{ get; set; }
...
}
public partial class Element
{
...
public Pan Pan { get; set; }
public Tray Tray { get; set; }
...
}
public class Tray
{
...
public virtual ICollection<Element> Elements{ get; set; }
...
}
我无法使用非空托盘加载元素列表。
new Context().Elements.Where(i => <my_filter>);
-> 延迟加载 -> tray = null 而我确实想要加载托盘!
new Context().Elements.Where(i => <my_filter>).Include(i => i.Tray).ToList();
-> 这很好用
好的。现在我需要获取特定 Pan 的元素,这带来了我未解决的问题。
myPan.Elements 是 ICollection 而不是 IQueryable。
所以我不能使用 .Include() 方法。
我的转换尝试没有成功:
myPan.Elements.AsQueryable<Element>().Where(i => <my_filter>).Include(i => i.Tray).ToList();
它运行但仍然延迟加载。
有什么想法吗?
实际上,您并没有被迫使用加载实体的导航属性,您可以使用带有附加过滤器的第二个(并且有效的)解决方案,如下所示:
new Context().Elements.Where(i => i.Pan.Id == myPan.Id && <my_filter>).Include(i => i.Tray).ToList();
或者,如果您愿意,只需插入 Where
,这是相同的:
new Context().Elements.Where(i => i.Pan.Id == myPan.Id).Where(i => <my_filter>).Include(i => i.Tray).ToList();
正在使用 Entity Framework 开发 WPF 应用程序。
我的实体 类 如下:
public partial class Pan
{
...
public virtual ICollection<Element> Elements{ get; set; }
...
}
public partial class Element
{
...
public Pan Pan { get; set; }
public Tray Tray { get; set; }
...
}
public class Tray
{
...
public virtual ICollection<Element> Elements{ get; set; }
...
}
我无法使用非空托盘加载元素列表。
new Context().Elements.Where(i => <my_filter>);
-> 延迟加载 -> tray = null 而我确实想要加载托盘!
new Context().Elements.Where(i => <my_filter>).Include(i => i.Tray).ToList();
-> 这很好用
好的。现在我需要获取特定 Pan 的元素,这带来了我未解决的问题。 myPan.Elements 是 ICollection 而不是 IQueryable。 所以我不能使用 .Include() 方法。
我的转换尝试没有成功:
myPan.Elements.AsQueryable<Element>().Where(i => <my_filter>).Include(i => i.Tray).ToList();
它运行但仍然延迟加载。
有什么想法吗?
实际上,您并没有被迫使用加载实体的导航属性,您可以使用带有附加过滤器的第二个(并且有效的)解决方案,如下所示:
new Context().Elements.Where(i => i.Pan.Id == myPan.Id && <my_filter>).Include(i => i.Tray).ToList();
或者,如果您愿意,只需插入 Where
,这是相同的:
new Context().Elements.Where(i => i.Pan.Id == myPan.Id).Where(i => <my_filter>).Include(i => i.Tray).ToList();