【问题标题】:Get all objects loaded in Current Session获取当前会话中加载的所有对象
【发布时间】:2013-07-16 02:32:36
【问题描述】:

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

我知道与会话关联的持久上下文缓存,包含当前会话中加载的所有对象的字典。谁能告诉我如何知道 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;                        
               }
          }
    }

【问题讨论】:

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


    【解决方案1】:

    我可能没有正确理解您的问题,因为如果您已经知道存在 PersistenceContext,这非常简单,但是您可以这样:

    ICollection entities = _session
        .GetSessionImplementation()
        .PersistenceContext
        .EntityEntries
        .Keys;
    

    【讨论】:

    • 它返回我 EntityEntry 对象的键。我想要我已知类型的对象。即我的实体类的对象。
    • @Deepak 仔细查看我的代码。我正在访问 EntityEntries 字典的 Keys 属性。此集合实际上包含实体对象,因为它们是此字典中的键(EntityEntry 对象是值)。
    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多