Entity Framework Find vs. Where

天涯浪子 提交于 2019-12-28 05:31:25

问题


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

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

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/16966213/entity-framework-find-vs-where

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!