繁体   English   中英

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

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

目前我正在使用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")不是解决方案。

注意:这仅适用于Fluent NHibernate下载 #632之后的版本

IOneToManyInstance上有一个名为Key的属性,允许您修改关系中使用的密钥; 在该属性上,有一个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