简体   繁体   English

实体框架数据库首先生成多对多中间表,但其中没有多余的列

[英]Entity Framework DB First generate many to many intermediate table WITHOUT having extra columns in it

It is clear to me that if I have a many to many relationship without other than the foreign key columns in the intermediate table, then EF will generate entities only for the main tables placing virtual collections in each to represent the relationship. 对我来说很清楚,如果我有一个多对多的关系,而中间表中没有外键列,那么EF只会为主表生成实体,在每个主表中放置虚拟集合来表示这种关系。 And when I have a many to many relationship containing extra columns EF will generate an entity for the intermediate table in order to give access to those extra columns. 当我与包含多余列的多对多关系时,EF将为中间表生成一个实体,以便提供对这些多余列的访问。 I need to know is there a way to have the intermediate table entity generated without having any extra columns in the table. 我需要知道有没有一种方法可以生成中间表实体而表中没有任何额外的列。

In other words: 换一种说法:

Let's assume we have the following relationship: Schools-StudentSchool-Students. 假设我们具有以下关系:Schools-StudentSchool-Students。 If I know the SchoolId how am I supposed to get all the students that DON'T study in that school without having the intermediate table. 如果我知道SchoolId,我应该如何在没有中间表格的情况下让所有不在那所学校学习的学生。 (In the object model having only a collection of the students currently studying in the school). (在对象模型中,只有当前在校学习的学生的集合)。 What am I missing in the whole picture? 整个图片中我缺少什么?

I believe that EF will remain with two tables if and only if the many-to-many relation is Total participation from both sides. 我相信,当且仅当多对多关系是双方的全面参与时,EF才会保留两个表格。

In such case, SQL: 在这种情况下,SQL:

Select *
From Students
Where StudentId Not IN( Select StudentId
                        From   Schools                    
                        Where  schoolId = x)   

or lambda 或lambda

var SchoolsRequested = Schools.Where(x=>x.schoolId = x);
var notWantedStudentIds = SchoolsRequested.Select(x=>x.StudentId).ToList();
Students.Where(x=> !notWantedStudents.Contains(x));

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

相关问题 实体框架更新多对多关系的中间表 - Entity Framework update the intermediate table of a many-to-many relation 实体框架代码首先使用额外的密钥进行多对多 - Entity framework code first many to many with extra key 实体框架与额外字段的多对多关系(数据库优先) - Entity Framework many to many relationship with extra field (Database First) 实体框架-在不加载导航属性的情况下向多表添加多行 - Entity Framework - Adding row many to many table without having to load navigational properties 如何使用 db first 方法在 Entity Framework 中编写多对多查询? - How to write many to many query in Entity Framework with db first approach? 实体框架代码优先的多对多表外键 - Entity Framework code-first foreign key to many to many table 首先在同一张表中启用Entity Framework 5代码中的多对多 - Enable multiple many to many in Entity Framework 5 code first to same table 实体框架代码首先使用自定义用户和映射表进行多对多 - Entity Framework Code First Many to Many with Custom User and Mapping Table 实体框架代码在单个表中的第一个多对多关系 - Entity Framework Code First Many to Many relationship(s) in single table 实体框架代码优先在同一个表上的多对多关系 - Entity Framework Code-first Many to many relationship on the same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM