That question, beforehand, is conceptual and has no relation to the Entity Framework itself.
About your doubt, you must think how Aluno
and Curso
: courses exist without students and students exist without courses; a student belongs to a course but a course belongs to your company.
In short, the best practice, in the mine point of view, is to create a table of interploration, basically as the CursoAluno
that you did, only better cut.
Let’s think:
- A student can been in more than one course?
- If yes, then the nomenclature is wrong:
CursosAluno
would be the best case, after all, a student may belong to more than one course simultaneously.
- Tables that interplorate should have their trivial and simple structure - this makes them flexible. Below, an example that I like and practice a lot, but that go probably require you foreign dependency keys to both the columns, because if one or the other - student or course - ceases to exist, the relationship - the 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, only how they interact with each other.
Cursosaluno.sql
+----+----------+----------+
| id | aluno_id | curso_id |
+----+----------+----------+
| 1 | 1 | 1 |
+----+----------+----------+
| 2 | 1 | 2 |
+----+----------+----------+
- If not, then you don’t need of that interploration table which is the current one
CursoAluno
. The only thing needed would be for you to put it on the table Alunos
a field called curso_id
- then you will be able to work with the desired flexibility.
Giving depth to your case, let’s consider the following:
Sql courses.
+----+----------------+
| id | name |
+----+----------------+
| 1 | C# |
+----+----------------+
| 2 | PHP |
+----+----------------+
| 3 | Banco de dados |
+----+----------------+
Sql students.
+----+----------------+
| id | name |
+----+----------------+
| 1 | João |
+----+----------------+
So let’s consider that John is martyred in C# and Database. Your table CursosAluno
will look like this:
+----+----------+----------+
| id | aluno_id | curso_id |
+----+----------+----------+
| 1 | 1 | 1 |
+----+----------+----------+
| 2 | 1 | 3 |
+----+----------+----------+
Now, you want to relocate him from C#
for PHP
, then we 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
.
This way, we will have the following result:
+----+----------+----------+
| id | aluno_id | curso_id |
+----+----------+----------+
| 1 | 1 | 2 |
+----+----------+----------+
| 2 | 1 | 3 |
+----+----------+----------+
If you think about it, we have a unanimous and independent structure, easy to maintain and with a simple concept.
When you change the course student, you would literally only mess with the table
CursoAluno
. I imagine that this table should have the student ID, course id and other data. You only touch it.– Tiedt Tech