强制加载导航属性 [英] Forcing eager-loading for a navigation property

查看:131
本文介绍了强制加载导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF Code First,我有一个名为Category的导航属性,我希望每次呼叫都加载:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆