Emerson Soares
Emerson Soares

Reputation: 474

Setting foreign key constraint name convention for all relationship types using Fluent Nhibernate

public class ReferenceConvention : IReferenceConvention{
 public void Apply(IManyToOneInstance instance) {
      instance.ForeignKey(string.Format("FK_{0}_{1}",
           instance.EntityType.Name,
           instance.Name));
 }

}

The above code allows me to change the naming convention for foreign key relationships in the case of the type References. How can I achieve the same result for IHasManyConvention name and IHasManyToManyConvention. The change of name convention for IReferenceConvention worked perfectly displayed the way, but I'm not getting to the other types of relationships.

Upvotes: 1

Views: 1174

Answers (1)

jenson-button-event
jenson-button-event

Reputation: 18951

For HasMany, I use:

    /// <summary>
/// Collection relationship constraint e.g. DocumentForum.Questions (1:m)
/// </summary>
public class HasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.ForeignKey(
            string.Format(
                "[FK {1}.{0}Id references {0}.Id {2}]",
                instance.EntityType.Name,
                instance.Relationship.Class.Name,
                instance.StringIdentifierForModel));

    }
}

Sorry cant help you with ManyToMany - i always express these as 2 x OneToMany (to a concrete linked entity) which get covered by the above

Upvotes: 2

Related Questions