views:

37

answers:

1

Hello everybody

I have the following code

factory = new Configuration().Configure().BuildSessionFactory();
session = factory.OpenSession();
var query = session.Linq<Root>();
var data = query.ToList<Root>();
var data2 = query.ToList<Root>();

This one generate 2 SQL queries, so i understand that first level cache isn't working. Does LINQ for nhibernate manage first level cache ? If yes, how to configure it ?

Thank's by advance.

+4  A: 

It is a misconception that the first level cache avoids execution of sql in general.

Queries are always executed on the database. This makes 100% sure that the result is exactly the same regardless if the instances are already in memory or not. This could be important for instance when using joins or conditions. There is no difference if the query actually contains joins or a where clause.

The first level cache make sure that the same instance is returned if it represents the same instance in the database. NHibernate executes the sql, when it finds the same object in memory as returned by the query, then it returns the one in the cache. But it can't know before executing the sql.

Only when you use session.Get or session.Load, NH does a cache lookup and avoids a query when it finds the instance.

AFAIK, Linq for NHibernate does nothing special with the cache.

Stefan Steinegger