1-many + many-1代码映射

时间:2012-10-11 06:45:28

标签: nhibernate fluent-nhibernate nhibernate-mapping

我有一种情况,我正在尝试使用1-m + m-1的代码映射来映射关系。 这源于先前的问题:One to Many mapping with an intermediate table

我能够使用Fluent NHibernate获得关系,但是有关于使用NH代码映射的讨论。我已经能够转换映射,以便插入工作就像Fluent版本一样,但是由于FK约束,Deletes失败了。

Fluent Mappings:

    public CustomerMap()
    {
        Schema("dbo");
        Table("Customer");
        Id(x => x.CustomerId);
        Map(x => x.CustomerNumber);
        Map(x => x.CreatedDate);
        Map(x => x.CreatedBy);
        Map( x => x.LastModifiedDate );
        Map( x => x.LastModifiedBy );
        HasMany(x => x.Addresses)
            .KeyColumn("ParticipantId")
            .Not.Inverse()
            .Not.KeyNullable()
            .Not.LazyLoad()
            .Cascade.All();
    }
        public CustomerAddressMap()
    {
        Schema("dbo");
        Table("CustomerAddress");
        Id(x => x.CustomerAddressId);
        Map(x => x.FromDate)
            .Not.Nullable();
        Map(x => x.ToDate);
        References(x => x.Address)
            .Column("AddressId")
            .Not.LazyLoad()
            .Cascade.All();
    }

}

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

根据我的理解,“参考”在客户地址和地址之间建立了一对多的关系。

代码映射:

        public CustomerMap()
    {
        Schema("dbo");
        Table("Customer");
        Id(x => x.CustomerId, a=>
                                    {
                                        a.Column("CustomerId");
                                        a.Generator(Generators.Identity);
                                    });
        Property(x => x.CustomerNumber);
        Property(x => x.CreatedDate);
        Property(x => x.CreatedBy);
        Property(x => x.LastModifiedDate);
        Property(x => x.LastModifiedBy);

        Bag(x => x.Addresses, c =>
                                {
                                    c.Key(k =>
                                            {
                                                k.Column("CustomerId");
                                                k.NotNullable(true);
                                            });
                                    c.Inverse(false);
                                    c.Cascade( Cascade.All);
                                    c.Lazy(CollectionLazy.NoLazy);
                                }, a => a.OneToMany() );
    }

    public CustomerAddressMap()
    {
        Schema( "dbo" );
        Table( "CustomerAddress" );
        Id( x => x.CustomerAddressId, a =>
        {
            a.Column( "CustomerAddressId" );
            a.Generator( Generators.Identity );
        } );
        Property( x => x.FromDate, a=>a.NotNullable(true) );
        Property( x => x.ToDate );
        ManyToOne(x => x.Address, a =>
                                    {
                                        a.Column("AddressId");
                                        a.Lazy(LazyRelation.NoLazy);
                                        a.Cascade(Cascade.All);
                                    });
    }

    public AddressMap()
    {
        Schema( "dbo" );
        Table( "Address" );
        Id( x => x.AddressId, a =>
        {
            a.Column( "AddressId" );
            a.Generator( Generators.Identity );
        } );
        Property( x => x.AddressType );
    }

我一直在为Cascade选项尝试不同的设置。 (全部+ DeleteOrphan),我也尝试为ManyToOne添加一个ForeignKey ...同时将NotNullable设置为True。据我所知,声明符合Fluent定义的任何我尝试的内容,它试图在CustomerAddress之前删除地址。

请注意,数据模式具有不可为空的FK。

0 个答案:

没有答案