如何使用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
提供此信息?
答案 0 :(得分:5)
是
Session.SessionFactory.GetClassMetadata(typeof(TEntity)).IdentifierPropertyName
是标识符的属性名称。
还有IdentifierType
,它是标识符的NH IType
。您可以使用SetIdentifier
和GetIdentifier
从标识符读取和写入值。