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();
}
您的回答不仅仅是回答。目的是匹配一个不是新的数据库。 –
@ Kris-I是的,对不起,应该是一个评论。顺便说一句,我编辑了我的答案,你最有可能混合流利的映射和自动映射的问题。 – cremor