其中与一个中间表很多映射 [英] One to Many mapping with an intermediate table

查看:104
本文介绍了其中与一个中间表很多映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我有这一个映射中介表作为的hasMany和中介和孩子之间的HasOne,但是HasOne预计一键分享挂钩。 (无逆选项:[)

I thought I had this one pegged by mapping the intermediary table as a HasMany and between intermediary and child as HasOne, however HasOne expects to share a key. (No Inverse option.:[ )

总之,关系结构我有:

地址(儿童)结果
AddressId结果
..Address场结果
结果
AddressCustomer(中介)结果
AddressCustomerId结果
AddressId结果
客户编号结果
结果
客户(家长)结果
客户编号结果
..Customer领域

Address (Child)
AddressId
..Address Fields

AddressCustomer (Intermediary)
AddressCustomerId
AddressId
CustomerId

Customer (Parent)
CustomerId
..Customer Fields

为什么我有这样的中介表而不是一个正常的一对多的?因为会有将需要包含地址等实体。 (即网站等),他们将有自己的中介表,以便它们可以共享地址表

Why I have this intermediary table instead of a normal 1-many? Because there will be other entities that will need to contain addresses. (I.e. Sites, etc.) They will have their own intermediary table so they can share the address table.

映射我到目前为止有:

    public class CustomerAddressMap : ClassMap<CustomerAddress>
{
    public CustomerAddressMap()
    {
        Schema("dbo");
        Table("CustomerAddress");
        Id(x => x.CustomerAddressId);
        Map(x => x.FromDate)
            .Not.Nullable();
        Map(x => x.ToDate);
        HasOne(x => x.Address)
            .ForeignKey("AddressId")
            .Cascade.All();
    }

}

public class AddressMap : ClassMap<Address>
{
    public AddressMap()
    {
        Schema("dbo");
        Table("Address");
        Id(x=>x.AddressId);
        Map(x => x.AddressType);
    }
}

通过在CustomerAddress表为空的AddressId列,行插入,但是AddressID从地址行不回传播到CustomerAddress。没有对HasOne没有逆选项,这样似乎是一个死胡同。我不能对CustomerAddress产生的地址ID,因为这会导致重复一次,我添加类似SiteAddress和必须做同样的事情。这招的可以的有GUID的作为键的工作,但我坚持通过自动递增整型的时刻。

With a null-able AddressId column in the CustomerAddress table, rows are inserted, however the AddressID from the Address row doesn't propagate back up to CustomerAddress. There is no Inverse option on HasOne so that seems to be a dead end. I cannot have the Address ID generated on CustomerAddress because this would result in duplicates once I add something like SiteAddress and have to do the same thing. That trick may work with GUIDs as keys, but I'm stuck with auto-increment Ints for the moment.

其他的一些想法我noodling在被映射CustomerAddress和地址的合并,但我不相信,与流利的NHibby支持。

Some other ideas I was noodling over was mapping a merge of CustomerAddress and Address but I don't believe that is supported with Fluent NHibby.

我想,这是一个问题域,有人已经成功应用。基本上我希望那里的孩子表(不是的记录)被多个父母之间共享的1一对多的关系。任何想法?

I figure it's a problem domain that someone has successfully applied. Essentially I want a 1-Many relationship where the child table (not it's records) is shared between multiple parents. Any ideas?

推荐答案

正常引用映射它

public class CustomerAddressMap : ClassMap<CustomerAddress>
{
    public CustomerAddressMap()
    {
        Table("CustomerAddress");

        Id(x => x.CustomerAddressId);
        Map(x => x.FromDate).Not.Nullable();
        Map(x => x.ToDate);
        References(x => x.Customer, "CustomerId");
        References(x => x.Address, "AddressId");
    }
}

这篇关于其中与一个中间表很多映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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