在NHibernate中,如何找到持久POCO类的id属性的名称,知道它的类型?

时间:2011-03-10 13:53:58

标签: nhibernate nhibernate-mapping

如何使用NHibernate类元数据获取实体类的id(主键)属性名称?假设我有以下类,我可以找到typeof(TEntity)的ID的属性名称和类型吗?

public abstract class Repository<TKey, TEntity> : IRepository<TKey, TEntity>
    where TEntity : class
{
    private static string PrimaryKeyName;
    // ...

    protected ISession Session { ... }
}

通常,属性的名称为Id,但在少数情况下,它会有所不同。我正在使用.hbm.xml映射。确实

Session.SessionFactory.GetClassMetadata(typeof(TEntity)).IdentifierPropertyName

提供此信息?

1 个答案:

答案 0 :(得分:5)

Session.SessionFactory.GetClassMetadata(typeof(TEntity)).IdentifierPropertyName 

是标识符的属性名称。

还有IdentifierType,它是标识符的NH IType。您可以使用SetIdentifierGetIdentifier从标识符读取和写入值。