I assume you need the NHibernate SchemaExport to create these indexes / keys for you.
For many-to-many packages (the default collection type is FluentNHibernate), SchemaExport generates:
create UserGroups ( securityGroupId INT not null, userId INT not null, )
For many-to-many sets, it generates:
create UserGroups ( securityGroupId INT not null, userId INT not null, primary key (securityGroupId, userId) )
... so just add .AsSet() to your mapping.
mapping.HasManyToMany(x => x.Members) .AsSet()
It actually makes sense if you are thinking about what bags and sets are. The elements of the set must be unique, and the bags do not have a uniqueness requirement.
Daniel Schilling
source share