Luke101
Luke101

Reputation: 65268

Does Linq to Entities Cache Queries?

I will be using Linq to Entities. The question that I have is, I will be calling Linq to Entities multiple times. Will Linq to Entities queries be cached will it is called several times? If not is there a way to cache the query so it is not compiled or generated every time it is called.

Upvotes: 4

Views: 3584

Answers (3)

NotDan
NotDan

Reputation: 32223

You can use a compiled query to keep the query from being generated every time. This will significantly improve performance if you are using the exact same query multiple times.

http://thedatafarm.com/blog/data-access/compiled-queries-in-entity-framework/

Upvotes: 1

Craig Stuntz
Craig Stuntz

Reputation: 126557

Generally speaking, not enough caching by default. There is a certain amount of compiled query caching which the Entity Framework does, but it does not live any longer than the ObjectContext. If you have short-lived ObjectContexts, like me, you will want something which lasts longer. That "something" is CompiledQuery.

Upvotes: 3

Ilya Khaprov
Ilya Khaprov

Reputation: 2524

Basically yes.
See here for details - > "you'll always get the same instance back whenever you requery for the object " One more link

UPDATE

what you means by compiled queries? Queries which return always the same set of object? queries which compiled to IL? queries which return the same instances?

Upvotes: 1

Related Questions