C# – NHibernate Second Level Cache With NHibernate Linq Provider 1.0

clinqnhibernate

How to enable NHibernate Second-Level Cache with NHibernate Linq Provider 1.0 ?

Second-Level Cache seems to work only with ICriteria usage.

Best Answer

Yes, I finally worked this one out:

public IQuerable<T> CreateLinqQuery()
{
    var query = session.Linq<T>();
    query.QueryOptions.SetCachable(true);
    return query;
}

Update As others have pointed out, in NH3, use query.Cacheable(). However be very careful to do it like this:

// Correct way:
query = query.Cacheable();

// This won't work:
query.Cacheable();