实体框架 - 延迟加载使用了ToList甚至工作() [英] Entity Framework - Lazy Loading working even with ToList()

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

问题描述

首先,我使用EF 6.0代码第一次的做法。
我的上下文配置设置为启用代理创建和延迟加载



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



我觉得下面的代码是更强的解释:

 公共无效测试()
{
VAR公司= GetCompanies()FirstOrDefault()。

如果(company.Employees.Count()0)
{
//我来到这里没有错误!
}
}

公开的IEnumerable<公司+ GT; GetCompanies()
{
VAR公司= context.Companies.ToList();
//注意,我并没有包括雇员(子表)

返回公司;
}



需要注意的是评论,我说:我来到这里没有错误。这意味着,懒加载了ToList()调用工作后均匀。我认为,转换后的IQueryable列出或IEnumerable的外汇基金将失去做延迟加载的能力。



我注意到,代理仍然是实体启用通过GetCompanies方法返回(在debbug模式,我可以看到那个丑陋的哈希值,如:System.Data.Entity.DynamicProxies.Company_7035BEA374959AC1 ...)。



延迟加载的作品甚至称它在不同的DLL。它是否正确?我的意思是,可以不同的DLL改变了我的数据库以后调用即使我的方法返回一个IEnumerable(而不是IQueriable)?



任何澄清,将不胜感激。


解决方案

需要注意的是评论,我说:我来到这里没有错误。 ,这意味着
的延迟加载了ToList()调用之后的工作,甚至




这就是延迟加载的整点:你可以从DB需要时,他们得到的实体(即当您访问的属性),而不是只有当你执行查询的第一次(即您的来电 .ToList())。




延迟加载的作品甚至称它在不同的DLL。这是
是否正确?我的意思是,可以不同的DLL改变了我的
数据库后续调用即使我的方法返回一个IEnumerable(而不是IQueriable)?




是的,它是正确的,但要小心,如果你处理你的情况下,延迟加载是不行的(它会抛出一个的ObjectDisposedException )。
此外,当您的代码将工作,你可能会因为生成的SQL请求数的性能问题。



附注:我个人推荐的不可以使用延迟加载。请参见 http://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 http://stackoverflow.com/a/21379510/870604

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

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