实体框架 - 懒惰加载工作甚至与ToList() [英] Entity Framework - Lazy Loading working even with ToList()

查看:108
本文介绍了实体框架 - 懒惰加载工作甚至与ToList()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我正在使用具有Code First方法的EF 6.0。
我的上下文配置设置为启用代理创建和延迟加载。



我的问题是:
延迟加载应该工作结果有一个返回IEnumerable(而不是IQueryable)的方法?



我认为以下代码更具说明性:

  public void Test()
{
var company = GetCompanies()。FirstOrDefault();

if(company.Employees.Count()> 0)
{
//我在这里没有错误!
}
}

public IEnumerable< Company> GetCompanies()
{
var company = context.Companies.ToList();
//注意我没有包括员工(子表)

退货公司;
}

请注意,我说:我没有错误地到达这里!这意味着即使在ToList()调用之后,延迟加载仍然可用。我认为在将IQueryable转换为List或IEnumerable之后,EF将失去延迟加载的能力。



我注意到代理仍然启用了由GetCompanies方法返回(在debbug模式下,我可以看到丑陋的哈希如:System.Data.Entity.DynamicProxies.Company_7035BEA374959AC1 ...)。



懒惰加载甚至在不同的DLL上调用它。它是否正确?我的意思是,即使我的方法返回一个IEnumerable(而不是IQueriable),我可以使用另一个DLL进行后续调用吗?



任何澄清都将不胜感激。 p>

解决方案


请注意,我说:我没有错误地到达这里!这意味着
,即使在ToList()调用后,懒惰加载也可以正常工作。


这是延迟加载的全部要点:您可以在需要时从DB获取实体(即当您访问该属性时),不仅在您首次执行查询时(即,您调用 .ToList())。


懒惰加载甚至可以在不同的DLL上调用它。这个
是否正确?我的意思是,即使我的方法返回一个IEnumerable(而不是IQueriable),我的
数据库中可以使用不同的DLL进行后续调用?




是的,这是正确的,但要小心,如果你处理你的上下文,懒惰加载将无法工作(它会抛出一个 ObjectDisposedException )。
另外,当您的代码可以工作时,由于生成的SQL请求数量可能会导致性能问题。



备注:我个人建议使用延迟加载。请参阅 https://stackoverflow.com/a/21379510/870604


First of all, I am using EF 6.0 with Code First approach. My context Configuration is set to Enable "Proxy Creation" and "Lazy Loading".

My question is: Does the lazy loading should work with results of an method that returns IEnumerable (and not IQueryable)?

I think the code below is more explanatory:

public void Test()
{
    var company = GetCompanies().FirstOrDefault();

    if (company.Employees.Count() > 0)
    {
        //I got here without errors!
    }
}

public IEnumerable<Company> GetCompanies() 
{
    var company = context.Companies.ToList();
    //Note that I did not Include the Employee (child table)

    return company;              
}

Note that comment that I say: "I got here without errors!". It means that lazy loading is working even after ToList() call. I thought that after convert an IQueryable to List or IEnumerable the EF would lost the capability of doing lazy loading.

I have noted that the Proxy still enabled for the entities that are returned by GetCompanies method (in debbug mode I can see that ugly hash like: System.Data.Entity.DynamicProxies.Company_7035BEA374959AC1...).

The lazy loading works even calling it on different DLL. Is this correct? I mean, can a different DLL made subsequent calls in my database even if my method return an IEnumerable (and not IQueriable)?

Any clarification will be greatly appreciated.

解决方案

Note that comment that I say: "I got here without errors!". It means that lazy loading is working even after ToList() call.

That's the whole point of lazy-loading: you can get entities from the DB when they are required (i.e. when you access to the property), not only when you execute the query for the first time (i.e. your call to .ToList()).

The lazy loading works even calling it on different DLL. Is this correct? I mean, can a different DLL made subsequent calls in my database even if my method return an IEnumerable (and not IQueriable)?

Yes it's correct, but be careful, if you dispose your context, lazy loading won't work (it'll throw an ObjectDisposedException). Also, while your code will work, you might have performance issues because of the number of SQL requests generated.

Side note: personally I recommend to not use lazy-loading. See https://stackoverflow.com/a/21379510/870604

这篇关于实体框架 - 懒惰加载工作甚至与ToList()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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