1
votes

I am using FluentNH with automapping and conventions. I set up a Many-to-Many convention as such:

    public class HasManyToManyConvention : IHasManyToManyConvention
    {
        public void Apply(IManyToManyCollectionInstance instance)
        {
            instance.Key.Column(instance.EntityType.Name + "Id");
            instance.Relationship.Column(instance.Relationship.StringIdentifierForModel + "Id");
        }
    } 

Assuming these are my tables: User(Id,Username,Password), Role(Id,Name)

Upon database generation, the associative table is being generated fine: UserInRole(UserId,RoleId)

However, it has no composite primary key set. Any idea how to fix this from the convention?

Anyone? Might it be this is currently not supported by Fluent?

1
Each call to instance.Relationship.Column() adds another to the existing columns that are in the instance.Relation.Columns collection so that explains why you're getting a composite primary key set. I'm currently looking into a way of resetting the columns but so far I've been out of luck.Sandor Drieënhuizen
Please refer to my related question: stackoverflow.com/questions/9772230/…Sandor Drieënhuizen
@SandorDrieënhuizen the referenced post has been deleted.Jeremy Holovacs

1 Answers

0
votes

Turns out the convention should remain the same. However, instead of using IList<User> and IList<Role> on my entity definitions, I'm now using ISet<User> and ISet<Role>. Now NHibernate will automatically create a composite primary key on the associative table.