How to use the IHasManyConvention Fluent NHibernate convention to set a PropertyRef for HasMany mapping?

I am currently using Fluent NHibernate to create my database schema, but I want the entities from the HasMany relation to point to another column for the link. IE, this is what NHibernate will generate in creating 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 will be the main key, and Stable.EntityId is another column that I set.

I already have a class 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 need to do to change the reference column?

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

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

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

+4
source share
1 answer

Note: this is only available in builds after # 632 of Downloading free NHibernate

There is a property on IOneToManyInstance called Key , which allows you to change the key used in the relation; there is a PropertyRef method on this property, which should be what you are looking for.

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

Source: https://habr.com/ru/post/1302528/


All Articles