我正在尝试使用以下行在运行时为POCO类加载nhibernate映射:
var persistentClass = NHibernateHelper.Configuration.GetClassMapping( type );
var property = persistentClass.GetProperty( propertyName );
它工作正常,但它在具有以下映射的类上的属性GroupId上失败:
<class name="GroupPartnerInterest" table="[GROUP_PARTNER_INTERESTS]">
<composite-id >
<key-property name="GroupId" column="PAR_ID" />
如果type == typeof(GroupPartnerInterest)
persistentClass.GetProperty( "GroupId" )
因MappingException失败:
未找到属性:实体上的GroupId GroupPartnerInterest“
我可以在调试器中看到来自key-properties
的{{1}}没有出现在persistentClass.properties中。
有没有办法获得此密钥属性的映射?
提前谢谢。
答案 0 :(得分:1)
普通属性可以通过persistentClass.PropertyClosureIterator
进行迭代(包括基类的属性)。
关键属性位于( ( Component )( persistentClass.Identifier ) ).PropertyIterator
。
因此,使用这段代码,我可以搜索关键属性和普通属性:
var propserties = persistentClass.PropertyClosureIterator;
if ( persistentClass.Identifier is Component )
{
properties = ( ( Component )( persistentClass.Identifier ) ).PropertyIterator
.Union( properties );
}
Property property
= (
from it in properties
where it.Name == propertyName
select it
).FirstOrDefault();