I have two entities: Employeees
and Projects which have the relationship of Many-to-Many. I defined a ProjectEmployee
for that, but when I add migration, it still creates a EmployeesProjects
. Why does this happen and how can I fix that? Thank you guys!
Here is the Employees.cs
using Day1.Model;
using System.ComponentModel.DataAnnotations;
public class Employees
{
[Required]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
public int DepartmentId { get; set; }
public DateTime JoinedDate { get; set; }
public Salaries Salary { get; set; }
public IList<Projects> Projects { get; set; }
public Departments Department { get; set; }
public IList<ProjectEmployee> ProjectEmployees { get; set; }
}
Projects.cs
using Day1.Model;
using System.ComponentModel.DataAnnotations;
public class Projects
{
[Required]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
public IList<Employees> Employees { get; set; }
public IList<ProjectEmployee> ProjectEmployees { get; set; }
}
The ProjectEmployee.cs
namespace Day1.Model
{
public class ProjectEmployee
{
public int ProjectId { get; set; }
public Projects Project { get; set; }
public int EmployeeId { get; set; }
public Employees Employee { get; set; }
public bool Enable { get; set; }
}
}
Here is the migration:
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace Day1.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Departments",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Departments", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Projects",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Projects", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Employees",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
DepartmentId = table.Column<int>(type: "int", nullable: false),
JoinedDate = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Employees", x => x.Id);
table.ForeignKey(
name: "FK_Employees_Departments_DepartmentId",
column: x => x.DepartmentId,
principalTable: "Departments",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "EmployeesProjects",
columns: table => new
{
EmployeesId = table.Column<int>(type: "int", nullable: false),
ProjectsId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_EmployeesProjects", x => new { x.EmployeesId, x.ProjectsId });
table.ForeignKey(
name: "FK_EmployeesProjects_Employees_EmployeesId",
column: x => x.EmployeesId,
principalTable: "Employees",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_EmployeesProjects_Projects_ProjectsId",
column: x => x.ProjectsId,
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ProjectEmployees",
columns: table => new
{
ProjectId = table.Column<int>(type: "int", nullable: false),
EmployeeId = table.Column<int>(type: "int", nullable: false),
Enable = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ProjectEmployees", x => new { x.ProjectId, x.EmployeeId });
table.ForeignKey(
name: "FK_ProjectEmployees_Employees_EmployeeId",
column: x => x.EmployeeId,
principalTable: "Employees",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ProjectEmployees_Projects_ProjectId",
column: x => x.ProjectId,
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Salaries",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
EmployeeId = table.Column<int>(type: "int", nullable: false),
Salary = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Salaries", x => x.Id);
table.ForeignKey(
name: "FK_Salaries_Employees_EmployeeId",
column: x => x.EmployeeId,
principalTable: "Employees",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.InsertData(
table: "Departments",
columns: new[] { "Id", "Name" },
values: new object[,]
{
{ 1, "Software Development" },
{ 2, "Finance" },
{ 3, "Accountant" },
{ 4, "HR" }
});
migrationBuilder.CreateIndex(
name: "IX_Employees_DepartmentId",
table: "Employees",
column: "DepartmentId");
migrationBuilder.CreateIndex(
name: "IX_EmployeesProjects_ProjectsId",
table: "EmployeesProjects",
column: "ProjectsId");
migrationBuilder.CreateIndex(
name: "IX_ProjectEmployees_EmployeeId",
table: "ProjectEmployees",
column: "EmployeeId");
migrationBuilder.CreateIndex(
name: "IX_Salaries_EmployeeId",
table: "Salaries",
column: "EmployeeId",
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "EmployeesProjects");
migrationBuilder.DropTable(
name: "ProjectEmployees");
migrationBuilder.DropTable(
name: "Salaries");
migrationBuilder.DropTable(
name: "Projects");
migrationBuilder.DropTable(
name: "Employees");
migrationBuilder.DropTable(
name: "Departments");
}
}
}
I tried manually deleting the EmployeesProjects table create code block in migration, but it would still generate it when I add migration. I searched every files to see if I configed the model anywhere, but the result wasn’t so positive.
Phương Nguyễn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Since I have already defined the ProjectEmployee entity, there’s no use to include
public IList<Employee> Employees { get; set; }
in Employees.cs and
public IList<Employee> Employees { get; set; }
in Projects.cs.
The system will automatically generate an entity to insert Many-To-Many relation, even when there has already been one.
Phương Nguyễn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.