MVC 1.0 + EF: db.EntitySet.where(something) 是否仍然返回表中的所有行?
在存储库中,我这样做:
public AgenciesDonor FindPrimary(Guid donorId) {
return db.AgenciesDonorSet.Include("DonorPanels").Include("PriceAdjustments").Include("Donors").First(x => x.Donors.DonorId == donorId && x.IsPrimary);
}
然后在同一存储库中的另一个方法中,这样:
AgenciesDonor oldPrimary = this.FindPrimary(donorId);
在调试器中,结果视图显示该表中的所有记录,但是:
oldPrimary.Count();
是1(应该是)。
为什么我看到检索到所有表条目,而不仅仅是 1 个?我认为行过滤是在数据库中完成的。
如果 db.EntitySet 确实将所有内容获取到客户端,那么使用 EF 保持客户端数据精简版的正确方法是什么?获取所有行不会扩展我正在做的事情。
In a repository, I do this:
public AgenciesDonor FindPrimary(Guid donorId) {
return db.AgenciesDonorSet.Include("DonorPanels").Include("PriceAdjustments").Include("Donors").First(x => x.Donors.DonorId == donorId && x.IsPrimary);
}
then down in another method in the same repository, this:
AgenciesDonor oldPrimary = this.FindPrimary(donorId);
In the debugger, the resultsview shows all records in that table, but:
oldPrimary.Count();
is 1 (which it should be).
Why am I seeing all table entries retrieved, and not just 1? I thought row filtering was done in the DB.
If db.EntitySet really does fetch everything to the client, what's the right way to keep the client data-lite using EF? Fetching all rows won't scale for what I'm doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将鼠标悬停在 AgenciesDonorSet 上,您将看到所有内容,因为 LINQ to Entities(或 SQL)使用延迟执行。当查询实际执行时,它只是检索计数。
如果您想查看为任何查询生成的 SQL,您可以添加以下代码:
You will see everything if you hover over the AgenciesDonorSet because LINQ to Entities (or SQL) uses delayed execution. When the query is actually executed, it is just retrieving the count.
If you want to view the SQL being generated for any query, you can add this bit of code:
实体集不实现 IQueryable,因此您使用的扩展方法是 IEnumerable 扩展方法。请参阅此处:
http:// /social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/121ec4e8-ce40-49e0-b715-75a5bd0063dc/
我同意这很愚蠢,而且我很惊讶更多的人没有这样做t对此没有抱怨。官方给出的理由:
Entity Set does not implement IQueryable, so the extension methods that you're using are IEnumerable extension methods. See here:
http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/121ec4e8-ce40-49e0-b715-75a5bd0063dc/
I agree that this is stupid, and I'm surprised that more people haven't complained about it. The official reason: