简体   繁体   English

如何与实体框架建立多对多的关系

[英]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 我有一个数据库,有电影表和国家表,我也做了很多关系的Movie_Country表

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 但是当我选择我的数据库并在我的.net项目中生成一个实体模型时,这种关系对于Movie和Movie_Country以及Country和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. 在Entity框架中,需要使用一个表来创建多对多关系,其中两个连接外键都是复合主键,而表中没有其他属性。

In other words make movieID and countryID your compound primary key and remove movie_country_id from T_Movie_Country and it should work. 换句话说,将movieID和countryID作为复合主键并从T_Movie_Country中删除movie_country_id,它应该可以工作。 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. 所以movieID必须是你T_Movie表中的主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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