急切加载不会加载关系

时间:2016-03-08 23:27:36

标签: c# entity-framework

我尝试使用Eagerly Loading来加载所需的数据。但它不起作用。当我使用时:

 Configuration.LazyLoadingEnabled = True;

它加载了所有关系表,但是我想限制它,所以我将true更改为false并尝试加载关系,但是没有加载。

这是我的代码:

     public CommonsController(ICreditApplicationsContext creditApplicationsContext)
    {

        if(creditApplicationsContext != null)
        {
            this.creditApplicationsContext = creditApplicationsContext;

         creditApplicationsContext.Years.Include("Makes");
         creditApplicationsContext.Makes.Include("Models");
         creditApplicationsContext.Models.Include("Trims");
         }
        else
        {
            this.creditApplicationsContext = new CreditApplicationsContext();

        }
        this.creditApplicationsRepository = new CreditApplicationsRepository(this.creditApplicationsContext);     
    }

我将此代码放在使用Context的页面中,我尝试将其放在Context中但同样的问题。

1 个答案:

答案 0 :(得分:1)

您没有将调用结果分配给变量:

var Years = creditApplicationsContext.Years.Include("Makes").ToList();

您可以在该行上设置断点并逐步执行,现在应填充Years并包含Makes个对象。