简体   繁体   中英

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. 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.

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");
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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