如何使用 IHasManyConvention Fluent NHibernate 约定在 HasMany 映射上设置 PropertyRef?

发布于 2024-08-22 23:12:41 字数 1170 浏览 3 评论 0原文

目前,我正在使用 Fluent NHibernate 生成数据库架构,但我希望 HasMany 关系中的实体指向不同的列以供参考。 IE,这就是 NHibernate 将在创建 DDL 中生成的内容:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (Id);

这就是我想要的:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (EntityId);

其中 Stable.ID 将是主键,而 Stable.EntityId 只是我设置的另一列。

我已经有一个如下所示的类:

public class ForeignKeyReferenceConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Cascade.All();
        //What goes here so that I can change the reference column?
    }
}

我需要做什么才能更改参考列?

作为一个例子,下面是 IReferenceConvention 的代码看起来像做同样的事情:

    public class MyReferenceConvention : IReferenceConvention
    {
        public void Apply(IManyToOneInstance instance)
        {
            instance.PropertyRef("EntityId");
            instance.Cascade.All();
        }
    }

编辑: instance.Key.Column("EntityId") 不是解决方案。

Currently I'm using Fluent NHibernate to generate my database schema, but I want the entities in a HasMany relationship to point to a different column for the reference. IE, this is what NHibernate will generate in the creation DDL:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (Id);

This is what I want to have:

alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (EntityId);

Where Stable.ID would be the primary key and Stable.EntityId is just another column that I set.

I have a class already that looks like this:

public class ForeignKeyReferenceConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Cascade.All();
        //What goes here so that I can change the reference column?
    }
}

What do I have to do to get the reference column to change?

As an example, here is what the code for IReferenceConvention looks like to do the same thing:

    public class MyReferenceConvention : IReferenceConvention
    {
        public void Apply(IManyToOneInstance instance)
        {
            instance.PropertyRef("EntityId");
            instance.Cascade.All();
        }
    }

EDIT:
instance.Key.Column("EntityId") is not the solution.

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

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

发布评论

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

评论(1

惟欲睡 2024-08-29 23:12:41

注意:这仅在 Fluent NHibernate 下载


注意:这仅在 Fluent NHibernate 下载 IOneToManyInstance 上名为 Key 的属性可让您修改关系中使用的键;在该属性上,有一个 PropertyRef 方法,这应该就是您正在寻找的方法。

public class ForeignKeyReferenceConvention : IHasManyConvention
{
  public void Apply(IOneToManyCollectionInstance instance)
  {
    instance.Key.PropertyRef("EntityId");
  }
}

Note: this is only available in the builds after #632 from the Fluent NHibernate downloads

There's a property on IOneToManyInstance that called Key that lets you modify the key used in the relationship; on that property, there's a PropertyRef method, which should be what you're looking for.

public class ForeignKeyReferenceConvention : IHasManyConvention
{
  public void Apply(IOneToManyCollectionInstance instance)
  {
    instance.Key.PropertyRef("EntityId");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文