Logo

NHibernate

The object-relational mapper for .NET

How to

This page is converted from the old nhforge.org Wiki. First published by: Ricardo Peres on 03-01-2011, Last revision by: Ricardo Peres on 03-04-2011

Enumerating First Level Cache Entities

How to enumerate local (first level cache) session entities:

public static class SessionExtensions
{
    public static IEnumerable<T> Local<T>(this ISession session)
    {
        ISessionImplementor impl = session.GetSessionImplementation();
        IPersistenceContext pc = impl.PersistenceContext;

        foreach (Object key in pc.EntityEntries.Keys)
        {
            if (key is T)
            {
                yield return ((T) key);
            }
        }
    }
}

//sample usage:
IEnumerable<Post> localPosts = session.Local<Post>();

© NHibernate Community 2016