1

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 (i.e. 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 (i.e. specify in fluent so I get the StoreProduct bridge table to have a PK of both ProductFK and StoreFK)?

Zach Saw
  • 4,308
  • 3
  • 33
  • 49

1 Answers1

0

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.

Zach Saw
  • 4,308
  • 3
  • 33
  • 49
Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60
  • Chained many-to-one is what I wanted to avoid. So there's no way then besides that? Makes no sense, especially when EF 4.1 Code First defaults to the behavior I desire. Why would it not enforce unique constraint on the FK pair by default? – Zach Saw May 04 '12 at 12:25
  • Can't be helped, you can use the table either to describe a relation, which is used for updating related data, or as a mapped class. You are looking for a hybrid that doesn't exist (to my knowledge). – Sebastian Edelmeier May 07 '12 at 08:35