简体   繁体   中英

NHibernate Fluent many-to-many mapping with multicolumn primary key in junction table

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 (ie the junction / link / bridge table).

For example (from Example.FirstProject in Fluent source),

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

In StoreProduct table, you'd have 2 columns:

ProductFK, StoreFK

This StoreProduct 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:

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));

Is that possible with Fluent (ie specify in fluent so I get the StoreProduct bridge table to have a PK of both ProductFK and StoreFK)?

Generating a primary key can only be done when you implement StoreProduct as a DataClass of its own, not as an association table. But that way you won't end up having a many-to-many but rather chained many-to-one relations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM