This question in advance is conceptual and has no relation to the Entity Framework itself.
About your question, you should think of Aluno
and Curso
: courses exist without students and students there are no courses; a student belongs to a course, but a course belongs to your company.
In short, the best practice in my point of view is to create an interploration table, basically like CursoAluno
you did, only more well-cut.
Let's think:
- Can a student be in more than one course?
- If so, then the nomenclature is wrong:
CursosAluno
would be the best case, after all, a student can belong to more than one course simultaneously.
- Tables that interpellate should have their structure trivial and simple - this makes them flexible. Here is an example that I like and practice a lot, but that will probably require foreign keys for both columns, because if one or the other - student or course - cease to exist, the relationship - in principle - must be undone. Remembering that with this structure, the other tables are free to be modified because they do not directly affect their cultures, just as they interact with each other.
CoursesAlumni.sql
+----+----------+----------+
| id | aluno_id | curso_id |
+----+----------+----------+
| 1 | 1 | 1 |
+----+----------+----------+
| 2 | 1 | 2 |
+----+----------+----------+
-
- If not, then you do not need this interlock table which is the current
CursoAluno
. The only thing required would be for you to put in the Alunos
table a field called curso_id
- so you'll be able to work with the desired flexibility.
Giving depth to your case, let's consider the following:
Courses.sql
+----+----------------+
| id | name |
+----+----------------+
| 1 | C# |
+----+----------------+
| 2 | PHP |
+----+----------------+
| 3 | Banco de dados |
+----+----------------+
Students.sql
+----+----------------+
| id | name |
+----+----------------+
| 1 | João |
+----+----------------+
So let's consider that John is marred in C # and Database . Your CursosAluno
table looks like this:
+----+----------+----------+
| id | aluno_id | curso_id |
+----+----------+----------+
| 1 | 1 | 1 |
+----+----------+----------+
| 2 | 1 | 3 |
+----+----------+----------+
Now, you want to reallocate it from C#
to PHP
, then we remove the line WHERE aluno_id = 1 AND curso_id = 1
and then add a new record to aluno_id = 1
and curso_id = 2
.
In this way, we will have the following result:
+----+----------+----------+
| id | aluno_id | curso_id |
+----+----------+----------+
| 1 | 1 | 2 |
+----+----------+----------+
| 2 | 1 | 3 |
+----+----------+----------+
If you stop to think, we have a unanimous, independent structure, easy maintenance and simple concept.