I’m coming from an NHibernate background to Entity Framework v4.0. When I load entities into the ObjectContext, I expect them to be cached there as long as the ObjectContext lives (‘1st level cache’). So, if the same query is executed the second time, the objects are already in the ObjectContext and should not be loaded again.
Have a look at this query:
using (var context = new Model1Container()) {
//load entities from DB
var entities = context.Entity1Set.ToArray();
//entities should now be cached in the context (1st level cache)
//why does this call not use the cached items?
entities = context.Entity1Set.ToArray();
}
In SQL Server Profiler, I can clearly see that both ToArray() calls trigger a database query. Why does the second query need a DB roundtrip, as opposed to the NHibernate behavior?
Thanks!
EF 4.0 do not support any form of cache, neither first nor second level cache as NHibernate do.
One feature that resembles to NHibernate first-level cache was implemented in EF 4.1.
EF 4.1 Local Data
HTH
Riana