简体   繁体   English

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

[英]How do I use the IHasManyConvention Fluent NHibernate convention to set the PropertyRef on a HasMany mapping?

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. 目前我正在使用Fluent NHibernate生成我的数据库模式,但我希望HasMany关系中的实体指向不同的列以供参考。 IE, this is what NHibernate will generate in the creation DDL: IE,这就是NHibernate在创建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. 其中Stable.ID是主键,Stable.EntityId只是我设置的另一列。

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: 作为一个例子,这里是IReferenceConvention的代码看起来像做同样的事情:

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

EDIT: instance.Key.Column("EntityId") is not the solution. 编辑: instance.Key.Column("EntityId")不是解决方案。

Note: this is only available in the builds after #632 from the Fluent NHibernate downloads 注意:这仅适用于Fluent NHibernate下载 #632之后的版本

There's a property on IOneToManyInstance that called Key that lets you modify the key used in the relationship; IOneToManyInstance上有一个名为Key的属性,允许您修改关系中使用的密钥; on that property, there's a PropertyRef method, which should be what you're looking for. 在该属性上,有一个PropertyRef方法,应该是您正在寻找的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM