简体   繁体   中英

How to make many to many relationship with entity framework

i have a database and there is Movie Table and Country table and also i made a Movie_Country table for many to many relationship

but when i select my database and generate an entity model in my .net project this relationship returns to one to many for Movie and Movie_Country and also Country and Movie_Country

so i cannot add record to many to many table with code what can i do for generate model with many to many relationship as my database diagram show

在此输入图像描述我的多对多图

In Entity framework many to many relationships need to be made with a table with both of the joining foreign keys as a compound primary key and no other properties on the table.

In other words make movieID and countryID your compound primary key and remove movie_country_id from T_Movie_Country and it should work. I also think that the foreign keys will have to be primary keys in their respective tables. So movieID would have to be your primary key in your T_Movie table.

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