【问题标题】:Fluent nHibernate mapping opposite side of ReferencesFluent nHibernate 映射参考的对面
【发布时间】:2011-05-23 07:58:08
【问题描述】:

我有一个类似于下面的表结构:

AttributeKey (ID, Title)
AttributeValue (ID, AttributeKeyID, Value)

我已经完成了所有映射设置,因此如果我查询 AttributeValue,它会使用引用连接到 AttributeKey (AttributeValue.AttributeKeyID = AttributeKey.ID),没问题……但是,我想查询 AttributeKey 表和 LEFT加入 AttributeValue 表,以便我获得所有 AttributeKeys 以及与它们相关的任何值......如何在流利的 nHibernate 中映射它?

本质上它与引用映射相反。

编辑:我知道 HasMany 方法与 References 相反,但使用它会引发我的属性未实现 UserCollectionType 的异常

干杯

【问题讨论】:

    标签: nhibernate fluent-nhibernate


    【解决方案1】:

    这应该是 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();
        }
    }
    

    【讨论】:

    • 谢谢科尔。我在那里执行的查询每个 AttributeKey 只会有一个或 0 个 AttributeValues - 我是否必须将它映射到 IList 属性才能工作?
    • 你的数据库设计没有指定一对一的关系。如果需要,您可以创建一个新的只读属性,该属性未映射到从 AttributeValues IList 中获取第一个元素(如果有元素)的数据库,否则返回 null。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 2011-10-26
    • 2011-01-29
    相关资源
    最近更新 更多