强制加载导航属性 [英] Forcing eager-loading for a navigation property
问题描述
public class Product
{
...
public Category Category {get;组; }
}
为了做到这一点,我必须把它包括在我要做的每一个电话在产品上
var results = from p in db.Products.Include(Category)select p;
有没有办法让Category属性加载,因此在每次调用中生成SQL连接,每次都要包含它?
谢谢
你可以使用@jeroenh提出的帮助方法,但它不会解决您所要求的所有订购产品及其类别的情况。 EF没有任何自动加载配置,例如Linq-to-sql中可用的配置。您必须始终使用 Include
(直接或通过一些帮助程序构建)。
I'm using EF Code First and I have a navigation property called Category that I want eager loaded in every call:
public class Product
{
...
public Category Category { get; set; }
}
To do this I have to include it in every call I'll do on Product
var results = from p in db.Products.Include("Category") select p;
Is there a way to have Category property eager loaded, therefore generation a SQL join, in every call without having to include it every time?
thank you
You can use helper method as proposed by @jeroenh but it will not solve situation where you for example want to load order with all ordered products and their categories. EF doesn't have any automatic eager loading configuration as for example available in Linq-to-sql. You must always use Include
(either directly or by some helper construction).
这篇关于强制加载导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!