views:

289

answers:

1

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.

+3  A: 

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");
  }
}
James Gregory
Nope. This code generates the following: alter table `Pony` add index (EntityId), add constraint Ponies_Stable foreign key (EntityId) references `Stable` (Id);
snicker
... who would DV the primary creator of FNH answering a question on it here.
Chris Marisic
@james I'm rifling through the source of FNH right now. There is a RO property: instance.Key.PropertyRef, which is really what I want to change as I am doing with a typical ReferenceConvention. Where can I modify this value, either explicitly or through convention?
snicker
Alright. So I just thought I lost my mind... in the FNH git repo, there is a commit on 10/31/09 by Paul Batum that adds a method for setting the PropertyRef on a KeyMapping in the OneToManyPart mapping class. However, this only allows the PropertyRef to be set in the class mapping and not through a convention... AND it's not in 1.0 RTM. I'll compile from source and see if I can get this working...
snicker
I can now do it manually in each of the class mappings with a HasMany mapping it it but not through a convention. The IOneToManyCollectionInstance class needs a similar treatment.
snicker
I've committed a fix for this and edited my answer accordingly.
James Gregory
Dear James Gregory: You are a hero among open-source software developers. I extend my extreme thanks.
snicker