我有两个实体:
Employeees
和项目,它们具有多对多的关系。我为此定义了一个 ProjectEmployee
,但是当我添加迁移时,它仍然会创建一个 EmployeesProjects
。为什么会发生这种情况以及如何解决?谢谢你们!
这里是
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; }
}
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; }
}
}
这是迁移:
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");
}
}
}
我尝试在迁移中手动删除EmployeesProjects表创建代码块,但当我添加迁移时它仍然会生成它。我搜索了每个文件,看看我是否在任何地方配置了模型,但结果并不那么积极。
由于我已经定义了 ProjectEmployee 实体,所以包含没有用
public IList<Employee> Employees { get; set; }
在Employees.cs 和
public IList<Employee> Employees { get; set; }
在 Projects.cs 中。 系统会自动生成一个实体来插入多对多关系,即使已经存在一个实体。