Junior — Middle
How can a many-to-many relationship be organized between entities in a database?
sobes.tech AI
Answer from AI
The 'many-to-many' relationship in a database is usually implemented through an intermediate table (join table), which contains foreign keys to both related entities.
For example, there are entities Student and Course. One student can attend many courses, and one course can have many students. For this, a StudentCourse table is created with two columns: StudentId and CourseId.
An example in C# using Entity Framework Core:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Course> Courses { get; set; }
}
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<Student> Students { get; set; }
}
// In EF Core 5 and above, you can configure the relationship without an explicit entity for the join table
Thus, the relationship is implemented through an additional table that stores pairs of related records.