实体框架查找与凡 [英] Entity Framework Find vs. Where

查看:107
本文介绍了实体框架查找与凡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有.Find(ID)和。凡(X => x.Id == ID),应该强迫我使用.Find()以上。凡/。首先()之间有显著差异?

Is there a significant difference between .Find(id) and .Where(x = >x.Id == id) that should compel me to use .Find() over .Where/.First()?

我会想象.Find会更有效,但它这么多有效的,我应该避免。凡()/。第一()?

I would imagine that .Find would be more efficient but is it so much more efficient that I should avoid .Where()/.First()??

我想问的原因是,我用在我的测试中一个通用FakeDbSet可以很容易实现的假成果,到目前为止,我还发现,我必须继承的类,并提供一个定制实现.Find(中),而如果我写我的code有。凡()/。第一()?我不需要做额外的工作。

The reason I ask is that I am using a generic FakeDbSet in my tests to make it easy to implement fake results and so far I have found that I must inherit that class and provide a custom implementation of .Find() whereas if I write my code with .Where()/.First()? I don't need to do that extra work.

推荐答案

的一点是,通过搜索范围内的本地缓存中查找开始,然后,如果没有匹配,将查询发送到数据库。

the point is that find starts by searching in the local cache of the context and then, if no match, sends a query to the db.

在那里总是将查询发送到数据库。

where always sends a query to the db.

使用EF 4 *,我曾经认为,通过发现生成的sql过于复杂,在某些情况下,会导致性能问题。所以,我总是用在哪里,即使EF 5.我应该检查以发现与EF 5生成的SQL。

With EF 4.*, I used to think that sql generated by find was too complex and, in some cases, leads to a performance issue. So I always use where even with EF 5. I should check the sql generated by find with EF 5.

于是在纸上,找到更好,因为他使用的缓存。

So in the paper, Find is better because he uses the cache.

这篇关于实体框架查找与凡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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