FluentNhibernate 和参考文献
我试图更改约定,以便我的 ID 遵循以下简单规则:ProductCode、CustomerCode、OrderCode 等。
我找到了一种简单的方法来添加约定:
public class PrimaryKeyNameConvention : IIdConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "Code");
}
}
现在我已经得到了我想要的,但似乎 FluentNhibernate 拒绝对引用我的主键的列应用相同的规则。 例如:我的表 Customer 将有一个名为 CustomerCode 的 PK,但我的表 Order 将有一个名为 Customer_Id 的参考列。 我尝试了不同的方法来重命名 CustomerCode 中的列 Customer_Id (表 Order),但似乎没有任何效果正常。 似乎唯一有效的解决方案是添加这样的约定:
public class ReferenceConvention : IReferenceConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
{
instance.Column(instance.Property.PropertyType.Name + "Code");
}
}
但现在 FluentNhibernate 创建了两列引用我的主键:CostumerCode 和 Customer_Id。
我不明白我做错了什么。 任何帮助将不胜感激。
问候,
阿尔贝托
I was trying to change a convention so that my IDs follow this simple rule: ProductCode, CustomerCode, OrderCode etc etc.
I've found a simple way to do that adding a convention:
public class PrimaryKeyNameConvention : IIdConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "Code");
}
}
Now I've got what I wanted but it seems that FluentNhibernate refuses to apply the same rule with column referencing my primary keys.
EX: my table Customer will have a PK called CustomerCode but my table Order will have a reference column called Customer_Id.
I've tried different ways to rename the column Customer_Id in CustomerCode (table Order) but it seems that nothing works properly.
The only solution which seems to work is adding a convention like this:
public class ReferenceConvention : IReferenceConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
{
instance.Column(instance.Property.PropertyType.Name + "Code");
}
}
but now FluentNhibernate creates two columns which reference my primary key: CostumerCode and Customer_Id.
I can't figure out what I am doing wrong.
Any help would be apreciated.
Regards,
Alberto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下 ForeignKeyConvention 基类。
Take a look at the ForeignKeyConvention base-class.
正如詹姆斯建议的那样,我现在已经应用了这两个约定:
并且一切正常。
As James suggested I've now applied these two conventions:
and everything works fine.