[英]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.