I always assumed that EF caches query results and that it would just return a list of entities from it’s internal cache, instead of executing the same query twice on the database.
var cipEntities = new CIPEntities(); // instantiate objectcontext
Console.WriteLine(cipEntities.Customers.ToList()); // fires db query
Console.WriteLine(cipEntities.Customers.ToList()); // also fires db query
Do I have the wrong assumption? Could it be that I am confused by my experience with RIA services, maybe RIA services does cache results and EF does not?
Thanks
Yes your assumption is wrong. EF doesn’t have second level cache (caching querires) so when you execute the query EF doesn’t know that it is a query already executed on the same context and it executes the query again. The main rule in EF – query is always executed even the result is already tracked by the context (and can be extracted from the context).
The solution is caching provider mentioned by @Robotsushi.