Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

.net - Entity Framework Find vs. Where

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Call to 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, led 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 on paper, Find() is better because it uses the cache.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...