Fluent NHibernate:如何创建一对多的双向映射? [英] Fluent NHibernate: How to create one-to-many bidirectional mapping?

查看:21
本文介绍了Fluent NHibernate:如何创建一对多的双向映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本问题:如何在 Fluent NHibernate 中创建双向一对多映射?

Basic question: How to I create a bidirectional one-to-many map in Fluent NHibernate?

详情:

我有一个有很多孩子的父对象.在我的情况下,孩子没有父母是没有意义的,所以在数据库中,我希望父母的外键具有 NOT NULL 约束.我正在从 Fluent NHibernate 映射自动生成我的数据库.

I have a parent object with many children. In my case, it is meaningless for the child to not have a parent, so in the database, I would like the foreign key to the parent to have NOT NULL constraint. I am auto-generating my database from the Fluent NHibernate mapping.

我有一个像这样有很多子对象的父对象:

I have a parent with many child objects like so:

public class Summary
{
   public int id {get; protected set;}

   public IList<Detail> Details {get; protected set;}
}

public  class Detail
{
   public int id {get; protected set;}

   public string ItemName {get; set;}

  /* public Summary Owner {get; protected set;} */ //I think this might be needed for bidirectional mapping?
}

这是我开始的映射:

public class SummaryMap : ClassMap<Summary>
{
    public SummaryMap()
    {
        Id(x => x.ID);

        HasMany<Detail>(x => x.Details);
    }
}

public class DetailMap : ClassMap<Detail>
{
    public DetailMap()
    {
        Id(x => x.ID);

        Map(x => x.ItemName).CanNotBeNull();
    }
}

在 Detail 表中,Summary_id 应该是 Not Null,因为在我的如果没有附加到 Detail 对象是没有意义的摘要对象.但是,仅使用 HasMany() 映射会使 Summary_id 外键为空.

In the Detail table, the Summary_id should be Not Null, because in my case it is meaningless to have a Detail object not attached to the summary object. However, just using the HasMany() map leaves the Summary_id foreign key nullable.

我在 NHibernate 文档中找到 (http://www.hibernate.org/hib_docs/nhibernate/html/collections.html) 表示如果需要父级,请使用双向一对多关联".

I found in the NHibernate docs (http://www.hibernate.org/hib_docs/nhibernate/html/collections.html) that "If the parent is required, use a bidirectional one-to-many association".

那么如何在 Fluent NHibernate 中创建双向一对多映射?

So how do I create the bidirectional one-to-many map in Fluent NHibernate?

推荐答案

要获得与 Details 表中非空外键列的双向关联,您可以添加建议的 Owner 属性,即 References(...).在DetailsMap类中映射CanNotBeNull(),使Summary结束反转.

To get a bidirectional association with a not-null foreign key column in the Details table you can add the suggested Owner property, a References(...).CanNotBeNull() mapping in the DetailsMap class, and make the Summary end inverse.

为了避免两个关联方向有两个不同的外键列,您可以手动指定列名,也可以为两个方向提供相同列名的方式命名属性.在这种情况下,我建议您将 Details.Owner 属性重命名为 Details.Summary.

To avoid having two different foreign key columns for the two association directions, you can either specify the column names manually or name the properties in a way that gives the same column name for both directions. In this case you I suggest renaming the Details.Owner property to Details.Summary.

我制作了由增量生成的摘要 id,以避免在插入表时出现问题,因为摘要当前除了 id 之外没有其他列.

I made the Summary id generated by increment to avoid problems when inserting into the table since Summary currenty has no columns besides id.

域:

public class Detail
{
    public int id { get; protected set; }
    public string ItemName { get; set; }

    // Renamed to use same column name as specified in the mapping of Summary.Details
    public Summary Summary {get; set;} 
}

public class Summary
{
    public Summary()
    {
        Details = new List<Detail>();
    }

    public int id { get; protected set; }
    public IList<Detail> Details { get; protected set; }
}

映射:

public class DetailMap : ClassMap<Detail>
{
    public DetailMap()
    {
        Id(x => x.id)
            .GeneratedBy.Native();

        Map(x => x.ItemName)
            .CanNotBeNull();

        References<Summary>(x => x.Summary)
            // If you don't want to rename the property in Summary,
            // you can do this instead:
            // .TheColumnNameIs("Summary_id")
            .CanNotBeNull();
    }
}

public class SummaryMap : ClassMap<Summary>
{
    public SummaryMap()
    {
        Id(x => x.id)
            .GeneratedBy.Increment();

        HasMany<Detail>(x => x.Details)
            .IsInverse()
            .AsBag(); // Use bag instead of list to avoid index updating issues
    }
}

这篇关于Fluent NHibernate:如何创建一对多的双向映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆