获取当前会话中加载的所有对象

时间:2013-07-17 13:45:26

标签: c#-4.0 nhibernate fluent-nhibernate nhibernate-mapping

我希望在当前会话中加载所有持久对象。

我知道与会话关联的持久化上下文缓存,包含当前会话中加载的所有对象的字典。任何人都可以告诉我如何知道IPersistenceContext缓存中加载的所有对象吗?

//创建我们的NHibernate会话工厂

    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;                        
               }
          }
    }

1 个答案:

答案 0 :(得分:1)

我可能没有正确理解你的问题,因为如果你已经知道有一个PersistenceContext,那就非常简单了,但是你可以去了:

ICollection entities = _session
    .GetSessionImplementation()
    .PersistenceContext
    .EntityEntries
    .Keys;
相关问题