I want all the persistent object loaded in current session.
I know about persistent context caches associated with the session, contains dictionary of all the objects loaded in the current session. Can any one tell me the way to know all the object which are loaded in IPersistenceContext cache?
// create our NHibernate session factory
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
Employee emp;
// populate the database
using (var transaction = session.BeginTransaction())
{
emp = session.Query<Employee>().Where(x => x.Name == "Bargin Basin").FirstOrDefault();
var entries = session.GetSessionImplementation().PersistenceContext.EntityEntries;
foreach (var item in entries)
{
var entityEntry = entries[item];
//I want the objects of my type like..
//Employee persistedEmp = entityEntry as Employee;
}
}
}
I might not have understand your question correctly since it is very straightforward if you already know that there is a PersistenceContext, but here you go:
ICollection entities = _session
.GetSessionImplementation()
.PersistenceContext
.EntityEntries
.Keys;