Reputation: 8562
I think this should be simple, but I can't figure out how to do it. Suppose I have the following maps:
public class AnimalMap : ClassMap<Animal> { Id( x => x.Id); }
public class CatMap: SubclassMap<Cat> {
Extends<AnimalMap>();
Map(x => x.IsDomestic);
}
Which creates tables as I expect:
Animal
------
Id
Cat
----
AnimalId : FK to Animal (named FK3500ABA0D)
IsDomestic
As noted, the FK gets generated by the db and ends up as FK3500ABA0D. All I want to do is set the name of that constraint, but I can't find how to do it via Fluent NHibernate (or actually even plain NHibernate, for that matter).
So, what am I missing?
Upvotes: 13
Views: 3381
Reputation: 1
I had the same problem, the following works well for me:
public class JoinedSubclassForeignKeyConvention : IJoinedSubclassConvention
{
public void Apply(IJoinedSubclassInstance instance)
{
if (instance.Type.BaseType != null)
instance.Key.ForeignKey(string.Format("FK_{0}_{1}", instance.EntityType.Name, instance.Type.BaseType.Name));
}
}
Your foreign key constraint would then be named as FK_Cat_Animal
Upvotes: 0
Reputation: 3194
Fluent NH does allow this:
public class ReferenceConvention : IReferenceConvention{
public void Apply(IManyToOneInstance instance) {
instance.ForeignKey(string.Format("FK_{0}_{1}",
instance.EntityType.Name,
instance.Name));
}
}
You'd also need to implement IHasManyConvention
and IHasManyToManyConvention
in the same way as above.
Upvotes: 22
Reputation: 52725
I don't know if FluentNH supports it, but the XML is simple:
<joined-subclass name="Cat">
<key column="AnimalId" foreign-key="NameOfTheFK"/>
</joined-subclass>
Upvotes: 1