[英]How to load two navigation property in a single statement in order to avoid database round trips(if any)
What i want is to load two navigation property in my class through database context using entity framework...the way I can do it with Include method... And does it incur me two round trips or not to the db... 我想要的是使用实体框架通过数据库上下文在类中加载两个导航属性...我可以使用Include方法来实现的方式...并且是否会导致我两次往返或不往返于数据库...
Note: I dont want to load it with with EF Context's Include method
注意:我不想使用EF Context的Include方法加载它
Currently I'm doing it this way 目前我正在这样做
db.Entry(category).Reference(p => p.Section).Load();
db.Entry(category).Collection("SubCategories").Load();
I want to load this in one statement 我想在一个语句中加载
You can do this in one trip using EF. 您可以使用EF进行一次旅行。 Try something like this (making some assumptions on your data/object model):
尝试这样的事情(对数据/对象模型做一些假设):
db.Categories
.Where(x=> x.ID == category.ID)
.Select(x=> new {
Category = x,
Section = db.Sections.FirstOrDefault(y => y.ID == x.SectionID),
SubCategories = db.Categories.Where(y=> y.ParentCategoryID == x.ID)
}).FirstOrDefault();
This will give you one object back (in one trip to the database) that has properties hydrated for the Category, Section, and SubCategories. 这将使您返回一个对象(一次访问数据库),该对象具有为Category,Section和SubCategories混合的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.