EF4.1 CodeFirst:添加字段以连接表
我正在使用 EF 4.1 RC 和 CodeFirst/POCO 通过代码构建我的数据库。
想象一下像教师-学生这样的多对多关系(一位教师可以有许多学生,一位学生可以有许多教师)。因此,我有两个 POCO:(1) 教师和 (2) 学生。
当 EF 创建相应的表时,您最终将得到三个表:(1) 教师、(2) 学生和 (3) 一个额外的联接表。连接表恰好包含两个字段:Teacher_ID 和Student_ID。
我想知道我是否有机会在连接表中添加一个额外的字段,例如“成绩”(某个老师给某个老师的成绩)?
目前我不知道如何仅用两个 POCO 来实现这一点。
所以我想我能做的就是手动创建第三个 POCO (用于连接表),对吗?这肯定会起作用,但是我会失去像 oneTeacher.Students.First() 等很好的导航属性。这就是我仍在寻找另一种方法的主要原因。
I am using EF 4.1 RC and CodeFirst/POCO to build my database by code.
Imagine having a many-to-many relationship like Teachers-Students (one teacher can have many students, and one student may have many teachers). Accordingly I have two POCOs: (1) Teacher and (2) Student.
When EF creates the corresponding tables you will end up with three tables: (1)Teachers, (2) Students and (3) an extra join table. The join table contains exactly two fields: a Teacher_ID and a Student_ID.
I was wondering if I had any chance to add an extra field to the join table, e.g. "Grade" (the grade a certain teacher gives a certain teacher)?
Currently I have no idea how to achieve this with only two POCOs.
So I guess all I can do is create a third POCO (for the join table) manually, am I right? That will certainly work, but then I am losing nice navigation properties like oneTeacher.Students.First(), etc. That is the main reason why I am still looking for another way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是正确的,并且不仅仅适用于代码优先。如果连接表中有额外的字段,您会将其映射为实体。反之亦然,如果您想在连接表中添加额外的字段,则需要创建一个新实体,并为
Teacher
和Student
实体。无论如何,您都无法轻松访问Teacher.Students
和Student.Teachers
,而必须通过中间实体。或者,您可以考虑以不同的方式对数据库结构进行建模,并将额外信息提取到
Teacher
或Student
或第四个实体中。但这完全取决于您的场景。That's correct, and does not only apply to Code-first. If you have extra fields in your joining table, you will have it mapped as an entity. And vice-versa, if you want an extra field in your joining table, you need to create a new entity and have zero-or-one-to many or one-to-many navigation properties to the
Teacher
andStudent
entities. In any case, you lose the comfort of accessingTeacher.Students
andStudent.Teachers
and have to go via the intermediate entity.Alternatively, you could think about modeling the DB structure differently and extracting the extra info into the
Teacher
orStudent
or a fourth entity. But that depends entirely on your scenario.是的,连接表不能有有效负载,或者您需要将其分解为 2 个一对多关联,这将导致创建第三个实体来保存 PK 以及附加属性。
Yes, the join table cannot have a payload or you need to break it down to 2 one to many association which will result in creating a third entity to hold the PKs as well as the additional properties.
这是一个我还没有时间尝试的想法。也许您可以保持学生和教师班级不变,并添加第三个 POCO
StudentGrade
,其属性为Student
、Teacher
和Grade
。然后,您必须使用流畅的 API 来确保Student
和Teacher
之间的多对多关系以及StudentGrade
类映射到同一张桌子。希望有帮助
This is an idea I still haven't found time to try it out. Maybe you can keep your Student and Teacher classes as they are, and add a third POCO
StudentGrade
with propertiesStudent
,Teacher
andGrade
. Then you'll have to use the fluent API to make sure that both the many to many relation betweenStudent
andTeacher
and theStudentGrade
class map to the same table.Hope that helps