1

在代码下面,客户可以有多个地址。有一个一对多的关系。我会在地址表中作为喜欢FK一个名为“客户”,而不是“CUSTOMER_ID”字段流利NHibernate:约定/ KeyColumn

我”尝试添加: .KeyColumn("Customer")>没有变化

我试图用改变ForeignKeyConvention没有变化。

有什么想法?

public class CustomerMap : ClassMap<Customer> 
{ 
    protected CustomerMap() 
    { 
     Id(x => x.Id).Not.Nullable(); 
     Map(x => x.FirstName).Not.Nullable().Length(25); 
     Map(x => x.LastName); 
     HasMany<Address>(x => x.Addresses)     
      .KeyColumn("Customer") 
      .AsSet() 
      .Inverse() 
      .Cascade.AllDeleteOrphan(); 
    } 
} 

public class AddressMap : ClassMap<Address> 
{ 
    public AddressMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.City).Not.Nullable().Length(100); 
    } 
} 

public class ForeignKeyReferenceConvention : IHasManyConvention 
{ 
    public void Apply(IOneToManyCollectionInstance instance) 
    { 
     instance.Key.PropertyRef("EntityId"); 
    } 
} 

public void DBCreation() 
{ 
    FluentConfiguration config = Fluently.Configure() 
     .Database(MsSqlConfiguration.MsSql2008.ConnectionString("....")) 
     .Mappings(m => 
      m.AutoMappings 
       .Add(AutoMap.AssemblyOf<Customer>()) 
       .Add(AutoMap.AssemblyOf<Address>() 
        .Conventions.Setup(c => c.Add<ForeignKeyReferenceConvention>()) 
        ) 
       ); 

    config.ExposeConfiguration(
       c => new SchemaExport(c).Execute(true, true, false)) 
     .BuildConfiguration(); 
} 

回答

0

我从未使用过自动映射自己,但不ClassMap只能用“默认”流利映射(不自动映射)使用?你想使用流利的映射或自动映射?

为什么你的一对多反转,虽然你没有多对一的一面映射?

顺便说一句,那个约定的目的是什么? PropertyRef()不应该使用,除非绝对需要(NHibernate不能做一些优化)。

+0

您的回答不仅仅是回答。目的是匹配一个不是新的数据库。 –

+0

@ Kris-I是的,对不起,应该是一个评论。顺便说一句,我编辑了我的答案,你最有可能混合流利的映射和自动映射的问题。 – cremor