FluentNHibernate:自动映射 OneToMany 关系

发布于 2024-12-20 07:10:21 字数 493 浏览 1 评论 0原文

通过流畅的 nhibernate,我无法使用带有约定的自动映射,因为它向表中添加了额外的外键以进行关系。该问题在 stackoverflow.com/questions/6091654/ Fluentnhibernate-automapping-onetomany-relation- using-attribute-and-convention/7867516

但正如你所看到的,通过使用属性来解决。我的问题是:

我不想在 Model Properties 上使用属性。因为接下来的几年我们可能不会在项目中使用nhibernate。所以我不想碰模特。没有KeyColumnAttribute的问题有没有解决方案。

谢谢

via fluent nhibernate, I cant use automapping with conventions because it adds extra foreign key to the table for relation. The problem was explained in detail at stackoverflow.com/questions/6091654/fluentnhibernate-automapping-onetomany-relation-using-attribute-and-convention/7867516

But as you can see, it is solved by using attribute. My question is:

I dont want to use attribute on Model Properties . Because we maybe wont use nhibernate in the project in following years. So I dont want to touch Models. Is there a solution for the problem without KeyColumnAttribute.

Thanks

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

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

发布评论

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

评论(1

生活了然无味 2024-12-27 07:10:21
class BiDirectionalHasManyConvention : IReferenceConvention, IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        // looks for a Property which references the type containing the collection
        var referenceProperty = instance.ChildType.GetProperties()
            .First(prop => prop.PropertyType == instance.EntityType);

        instance.Key.Column(referenceProperty.Name + "Id");
    }

    // Optional, just to make sure the foreignkey column is propertyname + "Id"
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(instance.Name + "Id");
    }
}
class BiDirectionalHasManyConvention : IReferenceConvention, IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        // looks for a Property which references the type containing the collection
        var referenceProperty = instance.ChildType.GetProperties()
            .First(prop => prop.PropertyType == instance.EntityType);

        instance.Key.Column(referenceProperty.Name + "Id");
    }

    // Optional, just to make sure the foreignkey column is propertyname + "Id"
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(instance.Name + "Id");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文