流利的nHibernate映射参考的相反一侧

问题描述

| 我有一个类似于下面的表结构:
AttributeKey (ID,Title)
AttributeValue (ID,AttributeKeyID,Value)
我已经映射了所有设置,因此如果我查询AttributeValue,则可以使用References将其连接到AttributeKey(AttributeValue.AttributeKeyID = AttributeKey.ID),但是没问题...但是,我想查询AttributeKey表并将LEFT JOIN移至AttributeValue表,以便获得所有AttributeKey和与它们关联的任何值...如何在流利的nHibernate中映射此属性? 从本质上讲,这与“引用”映射相反。 编辑:我知道HasMany方法是反向引用,但使用它会抛出异常,即我的属性未实现UserCollectionType 干杯     

解决方法

这应该是您的AttributeKey ClassMap中的HasMany。您的课程应如下所示:
public class AttributeKey
{
    public int Id { get; set; }
    public string Title { get; set; }
    public IList<AttributeValue> AttributeValues { get; set; }
}
您的映射将如下所示:
public class AttributeKeyMap : ClassMap<AttributeKey>
{
    public AttributeKeyMap()
    {
        Id(x => x.Id,\"ID\");
        Map(x => x.Title);
        HasMany(x => x.AttributeValues)
            .KeyColumn(\"AttributeKeyID\")
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}