(流畅)nHibernate:"与引用一样,外键默认为 Author_id,您可以覆盖它

发布于 2024-11-29 23:23:13 字数 1540 浏览 2 评论 0 原文

..使用 KeyColumn 方法或使用约定更改默认行为。 “

这句话来自流畅的 nhibernate 映射指南。我不明白为什么它这样做,而不是,例如,使用 Author 类主键的正确(列)名称。

我知道一个解决方案,那就是使用.KeyColumn 就像我自己的映射中一样(我的问题出现在这段代码之后):

    public class SupplierMap : ClassMap<Supplier>
{

    public SupplierMap()
    {
        Table("Suppliers");
        LazyLoad();
        Id(x => x.SupplierID, "SupplierID");
        Map(x => x.CompanyName).Not.Nullable();
        Map(x => x.ContactName);
        Map(x => x.ContactTitle);
        Map(x => x.Address);
        Map(x => x.City);
        Map(x => x.Region);
        Map(x => x.PostalCode);
        Map(x => x.Country);
        Map(x => x.Phone);
        Map(x => x.Fax);
        Map(x => x.HomePage);
        HasMany(x => x.Products).KeyColumn("ProductID").Inverse().Cascade.All();
    }
}

产品映射是:

    public class ProductMap : ClassMap<Product> 
{

    public ProductMap() 
    {
    Table("Products");
    LazyLoad();
        Id(x => x.ProductID);
        References(x => x.Supplier, "SupplierID");
    Map(x => x.ProductName).Not.Nullable();
    Map(x => x.QuantityPerUnit);
        Map(x => x.UnitPrice);
    Map(x => x.UnitsInStock);
        Map(x => x.UnitsOnOrder);
        Map(x => x.ReorderLevel);
    Map(x => x.Discontinued).Not.Nullable();

    }
}

所以我的问题是,是否有更优雅的方法,我很失望地看到我的产品表上有两列?对于 id(ProductID 和Product_ID)。如上所述,这是通过 KeyColumn("ProductID") 修复的,有没有办法在产品映射中设置它,甚至在配置中设置?有一个约定”——这可能就是答案。

..with the KeyColumn method or change the default behaviour with a Convention. "

That quote is from the fluent nhibernate mapping guide. I can't understand why it does this instead of, for that example, using the correct (column) name for the Author class primary key.

I know one solution, which is to use .KeyColumn like it is here in my own mappings (my question is coming after this code):

    public class SupplierMap : ClassMap<Supplier>
{

    public SupplierMap()
    {
        Table("Suppliers");
        LazyLoad();
        Id(x => x.SupplierID, "SupplierID");
        Map(x => x.CompanyName).Not.Nullable();
        Map(x => x.ContactName);
        Map(x => x.ContactTitle);
        Map(x => x.Address);
        Map(x => x.City);
        Map(x => x.Region);
        Map(x => x.PostalCode);
        Map(x => x.Country);
        Map(x => x.Phone);
        Map(x => x.Fax);
        Map(x => x.HomePage);
        HasMany(x => x.Products).KeyColumn("ProductID").Inverse().Cascade.All();
    }
}

And the Product mapping is:

    public class ProductMap : ClassMap<Product> 
{

    public ProductMap() 
    {
    Table("Products");
    LazyLoad();
        Id(x => x.ProductID);
        References(x => x.Supplier, "SupplierID");
    Map(x => x.ProductName).Not.Nullable();
    Map(x => x.QuantityPerUnit);
        Map(x => x.UnitPrice);
    Map(x => x.UnitsInStock);
        Map(x => x.UnitsOnOrder);
        Map(x => x.ReorderLevel);
    Map(x => x.Discontinued).Not.Nullable();

    }
}

So my question is, is there a more elegant way? I was quite disappointed to see two columns on my product table for id (ProductID and Product_ID). As mentioned this was fixed with KeyColumn("ProductID") as above. Is there a way I can set this in the Product mapping, or even in the configuration? I don't think I understand the "override the default behaviour with a convention" -- that is probably where the answer is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

安静被遗忘 2024-12-06 23:23:13

首先,如果您的 FK 全部采用 ObjectNameID 格式,那么您可能想要执行自定义 FK 约定:

public class CustomForeignKeyConvention : ForeignKeyConvention
{
    protected override string GetKeyName(Member property, Type type)
    {
        if (property == null)
            return type.Name + "Id";

        return property.Name + "Id";
    }
}

这应该可以使您不必手动指定 FK 名称。如果您不使用 Id 作为 PK 的属性名称,您也可以进行 PK 约定。

public class PrimaryKeyConvention : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        instance.Column(instance.EntityType.Name + "Id");
    }
}

(摘自 FNH 文档

我怀疑您使用的 PK 约定与 FNH 的期望不符本例中的问题。

First off, if your FKs are all in that format ObjectNameID then you probably want to do a custom FK convention:

public class CustomForeignKeyConvention : ForeignKeyConvention
{
    protected override string GetKeyName(Member property, Type type)
    {
        if (property == null)
            return type.Name + "Id";

        return property.Name + "Id";
    }
}

That should preclude you from having to specify the FK name manually. You can also do a PK convention if you're not using Id as the property name for your PKs.

public class PrimaryKeyConvention : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        instance.Column(instance.EntityType.Name + "Id");
    }
}

(from the FNH docs)

I suspect your use of a PK convention that doesn't match FNH's expectations is your issue in this case.

能否归途做我良人 2024-12-06 23:23:13

您需要将 Product.Id 的列名称指定为 ProductID

您所做的大部分操作都可以使用 AutoMapper 完成

You need to specify the column name of Product.Id as ProductID

Most of what you're doing can be done with the AutoMapper

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