实体框架:关闭延迟加载

时间:2016-04-20 20:17:08

标签: entity-framework lazy-loading

我有以下代码以惰性模式加载实体,我试图通过添加这行代码将延迟加载设置为false

context.Configuration.LazyLoadingEnabled = false;

到函数的顶部,看看会发生什么,我期望抛出一个异常,但没有发生任何事情并且代码正常执行,所以任何人都可以解释当我关闭延迟加载时会发生什么。

public List<Report> GetReportsByRole(int numberOfRecords, int pageNumber)
{
    if (CurrentUser.Role == 3)
    {
        return context.Reports.Where(report => report.ReportCreator.DepartmentOfTheUser.Company == CurrentUser.Company).OrderByDescending(report => report.Date).Skip(pageNumber * numberOfRecords).Take(numberOfRecords).ToList();
    }
    else
        if (CurrentUser.Role == 2)
        {
            return context.Reports.Where(report => report.ReportCreator.Depratment == CurrentUser.Department).OrderByDescending(report => report.Date).Skip(pageNumber * numberOfRecords).Take(numberOfRecords).ToList();
        }
        else
        {
            return context.Reports.Where(report => report.CreatedBy == CurrentUser.Id).OrderByDescending(report => report.Date).Skip(pageNumber * numberOfRecords).Take(numberOfRecords).ToList();
        }
}

0 个答案:

没有答案