views:

446

answers:

3

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.

A: 

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?

Trickster
Your first link is relevant to LinqToSql, not LinqToEntities. Your second link implies that, with some amount of additional work beyond what is provided out of the box, query caching can be made to work. An answer of "basically yes" seems unclear given that there are multiple questions and "basically yes" would not appear to be the answer to all of them.
Michael Maddox
ok, here it is:http://blogs.msdn.com/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspxhttp://blogs.msdn.com/alexj/archive/2009/04/22/tip-14-caching-entity-framework-reference-data.aspx
Trickster
+2  A: 

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.

Craig Stuntz
A: 

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/

NotDan