簡體   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