1

中間テーブルをHasManyとしてマッピングし、中間テーブルと子の間をHasOneとしてマッピングすることで、これをペグしたと思いましたが、HasOneはキーを共有することを期待しています。(反転オプションなし:[ )

とにかく、私が持っているリレーショナル構造:

Address (子)
AddressId
..Address フィールド

AddressCustomer (中間)
AddressCustomerId
AddressId
CustomerId

Customer (親)
CustomerId
..Customer フィールド

通常の 1-many ではなく、この中間テーブルがあるのはなぜですか? アドレスを含める必要があるエンティティが他にもあるためです。(つまり、サイトなど) アドレス テーブルを共有できるように、独自の中間テーブルがあります。

私がこれまでに持っているマッピング:

    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 テーブルに null 許容の AddressId 列がある場合、行が挿入されますが、Address 行の AddressID は CustomerAddress に反映されません。HasOne には Inverse オプションがないため、行き止まりのようです。CustomerAddress でアドレス ID を生成することはできません。これは、SiteAddress のようなものを追加して同じことをしなければならない場合に重複が発生するためです。このトリックGUID をキーとして機能する可能性がありますが、私は今のところ、自動インクリメントの Int にこだわっています。

私が考えていた他のアイデアは、CustomerAddress と Address のマージをマッピングすることでしたが、それが Fluent NHibby でサポートされているとは思えません。

誰かがうまく適用したのは問題のあるドメインだと思います。基本的に、子テーブル(レコードではない)が複数の親間で共有される1対多の関係が必要です。何か案は?

4

2 に答える 2

1

通常の参照としてマップします

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");
    }
}
于 2012-10-05T09:46:55.203 に答える
0

Why won't you make one-to-many from address to customer? Customer will contain foreign key then and address entity could be referened by other entity. In this case, you simply make References(x => x.Address); on customer table.
When I was trying to create one-to-one relation using FluentNHibernate, I faced common problems too. If you want to leave DB structure as you showed, I guess you should try the following mapping (with adding corresponding entity fields):

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

Such mapping left in my case one side empty, so I modified property setter for child entity (CustomerAddress):

public virtual Address Address
{ 
    get { return _address; }
    set 
    {
        _address = value;
        value.CustomerAddress = this;
    }
}

After these actions one-to-one worked fine) Hope it will help you to solve the problem.

于 2012-10-05T09:31:01.573 に答える