06_Relations between tables - Maniconserve/EF-Core GitHub Wiki

In the Code First Approach, relationships between tables are established using navigation properties and data annotations or Fluent API configurations. Below are different types of relationships you can define.


Here we are using navigation properties to establish relationships

1. One-to-One Relationship

A one-to-one relationship exists when a record in one table is linked to exactly one record in another table.

Example: Student and StudentAddress

public class Student  
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public StudentAddress Address { get; set; }  
}  

public class StudentAddress  
{  
    public int Id { get; set; }  
    public string City { get; set; }  
    public int StudentId { get; set; }  
    public Student Student { get; set; }  
}  
add-migration OneToOneRelation  
update-database

Generated Migration:

protected override void Up(MigrationBuilder migrationBuilder)  
{  
    migrationBuilder.CreateTable(  
        name: "StudentAddresses",  
        columns: table => new  
        {  
            Id = table.Column<int>(nullable: false)  
                .Annotation("SqlServer:Identity", "1, 1"),  
            City = table.Column<string>(nullable: true),  
            StudentId = table.Column<int>(nullable: false)  
        },  
        constraints: table =>  
        {  
            table.PrimaryKey("PK_StudentAddresses", x => x.Id);  
            table.ForeignKey(  
                name: "FK_StudentAddresses_Students_StudentId",  
                column: x => x.StudentId,  
                principalTable: "Students",  
                principalColumn: "Id",  
                onDelete: ReferentialAction.Cascade);  
        });  
}  

image

2. One-to-Many Relationship

A one-to-many relationship means a single record in one table is related to multiple records in another table.

Example: Teacher and Students

public class Teacher  
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public List<Student> Students { get; set; }  
}  

public class Student  
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public int TeacherId { get; set; }  
    public Teacher Teacher { get; set; }  
}  
add-migration OneToManyRelation  
update-database 

Generated Migration:

protected override void Up(MigrationBuilder migrationBuilder)  
{  
    migrationBuilder.AddColumn<int>(  
        name: "TeacherId",  
        table: "Students",  
        nullable: false,  
        defaultValue: 0);  

    migrationBuilder.CreateIndex(  
        name: "IX_Students_TeacherId",  
        table: "Students",  
        column: "TeacherId");  

    migrationBuilder.AddForeignKey(  
        name: "FK_Students_Teachers_TeacherId",  
        table: "Students",  
        column: "TeacherId",  
        principalTable: "Teachers",  
        principalColumn: "Id",  
        onDelete: ReferentialAction.Cascade);  
}  

image

3. Many-to-Many Relationship

A many-to-many relationship requires a junction table that connects two entities.

Example: Students and Courses

public class Student  
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public List<Course> Courses { get; set; }  
}  

public class Course  
{  
    public int Id { get; set; }  
    public string Title { get; set; }  
    public List<Student> Students { get; set; }  
}  
add-migration ManyToManyRelation  
update-database

Generated Migration:

protected override void Up(MigrationBuilder migrationBuilder)  
{  
    migrationBuilder.CreateTable(  
        name: "StudentCourse",  
        columns: table => new  
        {  
            StudentsId = table.Column<int>(nullable: false),  
            CoursesId = table.Column<int>(nullable: false)  
        },  
        constraints: table =>  
        {  
            table.PrimaryKey("PK_StudentCourse", x => new { x.StudentsId, x.CoursesId });  
            table.ForeignKey(  
                name: "FK_StudentCourse_Students_StudentsId",  
                column: x => x.StudentsId,  
                principalTable: "Students",  
                principalColumn: "Id",  
                onDelete: ReferentialAction.Cascade);  
            table.ForeignKey(  
                name: "FK_StudentCourse_Courses_CoursesId",  
                column: x => x.CoursesId,  
                principalTable: "Courses",  
                principalColumn: "Id",  
                onDelete: ReferentialAction.Cascade);  
        });  
}  

image


Check the git repo

These are the different types of relationships you can define in Code First Approach using Entity Framework Core.

⚠️ **GitHub.com Fallback** ⚠️