NHibernate Fluent ManyToMany mapping with multicolumn primary key for bridge table

160 views
Skip to first unread message

Zach Saw

unread,
May 4, 2012, 7:04:09 AM5/4/12
to Fluent NHibernate
I noticed the DDL that gets generated by Fluent doesn't create PK for
the 2 columns that make up the table for many-to-many relationship.

For example (from the first example),

Store <-- many-to-many --> Product

In StoreProduct table, you'd have 2 columns:

ProductFK, StoreFK

This StoreProduct table (i.e. the Store-Product bridge table) is
implicitly generated via Fluent's HasManyToMany statement. I'd like to
make it generate DDL that defines the 2 columns as PK.

This is what gets generated by Fluent for SQLite at the moment:

create table StoreProduct
(ProductFK INT not null,
StoreFK INT not null,
constraint FKE9A26716DC700501 foreign key (StoreFK) references
"Store",
constraint FKE9A26716815A48A8 foreign key (ProductFK) references
"Product");

I'd like it to do this:

create table StoreProduct
(ProductFK INT not null,
StoreFK INT not null,
constraint FKE9A26716DC700501 foreign key (StoreFK) references
"Store",
constraint FKE9A26716815A48A8 foreign key (ProductFK) references
"Product",
PRIMARY KEY(ProductFK, StoreFK));

I understand that EF 4.1 Code First automatically does that without
any additional instructions. In fact, shouldn't this be the default?

Is that even possible with Fluent?

Thanks!

Rasmoo

unread,
May 6, 2012, 4:10:58 PM5/6/12
to Fluent NHibernate
That's odd, since I get exactly that composite primary key behaviour
on both SQLite and MsSql dialects. E.g.

create table TableA_TableB (
TableA_id UNIQUEIDENTIFIER not null,
TableB_id UNIQUEIDENTIFIER not null,
primary key (TableA_id, TableB_id)
)

Note that it really isn't Fluent NHibernate which generate the DDL for
SQLite. It's done by the SchemaExport class together with the database
Dialect.

I'm curious about how you've mapped your many-to-many. Are you using a
HasManyToMany on both Proudct and Store?
Reply all
Reply to author
Forward
0 new messages