FluentNhibernate 和参考文献

发布于 2024-09-14 16:37:51 字数 1247 浏览 4 评论 0原文

我试图更改约定,以便我的 ID 遵循以下简单规则:ProductCodeCustomerCodeOrderCode 等。
我找到了一种简单的方法来添加约定:

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 创建了两列引用我的主键:CostumerCodeCustomer_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄凉少年不暖心 2024-09-21 16:37:51

看一下 ForeignKeyConvention 基类。

ForeignKeyConvention 是几个其他约定的合并,提供了一种简单的方法来指定域中所有外键的命名方案。这特别有用,因为并非所有外键都可以以相同的方式访问,具体取决于它们所在的位置;这个约定不需要了解底层结构。

Take a look at the ForeignKeyConvention base-class.

The ForeignKeyConvention is an amalgamation of several other conventions to provide an easy way to specify the naming scheme for all foreign-keys in your domain. This is particularly useful because not all the foreign-keys are accessible in the same way, depending on where they are; this convention negates need to know about the underlying structure.

昔日梦未散 2024-09-21 16:37:51

正如詹姆斯建议的那样,我现在已经应用了这两个约定:

public class PrimaryKeyNameConvention : IIdConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
    {
        instance.Column(instance.EntityType.Name + "Code");
    }
}

public class CustomForeignKeyConvention : ForeignKeyConvention
{
    protected override string GetKeyName(Member property, Type type)
    {
        if (property == null)
            return (type.Name + "Code");    // many-to-many, one-to-many, join

        return (property.Name + "Code");    // many-to-one
    }
}

并且一切正常。

As James suggested I've now applied these two conventions:

public class PrimaryKeyNameConvention : IIdConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
    {
        instance.Column(instance.EntityType.Name + "Code");
    }
}

public class CustomForeignKeyConvention : ForeignKeyConvention
{
    protected override string GetKeyName(Member property, Type type)
    {
        if (property == null)
            return (type.Name + "Code");    // many-to-many, one-to-many, join

        return (property.Name + "Code");    // many-to-one
    }
}

and everything works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文