实体框架可以处理多对多的关系没有交集的对象

实体框架可以处理多对多的关系没有交集的对象

本文介绍了实体框架可以处理多对多的关系没有交集的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数据库的第一个模型:假设我们有经典的表学生课程 StudentCourse (后者显然有FKS到学生课程)。

Using database first model: Let's say we have the classic tables Student, Course and StudentCourse (the latter obviously having FKs to Student and Course).

如果你把这个模式EF,你会得到他们每个人产生的对象。在学生课程类将各有集合 StudentCourses ,从中你需要跳到另一种关系才能到课程学生,分别为。

If you import this model to EF, you will get an object generated for each of them. The Student and Course classes will each have a collection of StudentCourses, from which you need to jump another relationship to get to the Course or Student, respectively.

我想在这样的方式的基本交集表是不可见的,即学生生成的代码集合课程课程学生集合。我已经看到了其他的ORM软件(具体而言,)这件事。可它EF做?

I would like to have the code generated in such a way that the underlying intersection table is invisible, i.e. Student has a collection of Courses, and Course has a collection of Students. I have seen this done in other ORM software (specifically, TopLink). Can it be done in EF?

推荐答案

据的,你会得到期望的行为,如果你的 StudentCourse 表只包含外键列。如果它包含任何其他列,EF将产生一个中间实体代表参加。

According to this tutorial, you'll get the desired behaviour if your StudentCourse table only contains the foreign-key columns. If it contains any other columns, EF will generate an intermediate entity to represent the join.

在这种情况下,放弃从 StudentCourse代理键桌子和一个复合主键应该工作取代它。

In this case, dropping the surrogate key from the StudentCourse table and replacing it with a composite primary key should work.

这篇关于实体框架可以处理多对多的关系没有交集的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 22:05