【发布时间】:2017-03-10 17:38:36
【问题描述】:
我正在使用SapientGuardian's EFCore library 将 EF Core 与 MySQL 一起使用,并且我正在尝试使用以下代码创建具有多对多关系的表;
public class Personnel
{
public int Id { get; set; }
[MaxLength(100)]
public string Name { get; set; }
public virtual ICollection<PersonnelDegree> PersonnelDegrees { get; set; }
}
public class Degree
{
public int Id { get; set; }
[MaxLength(100)]
public string Name { get; set; }
public virtual ICollection<PersonnelDegree> PersonnelDegrees { get; set; }
}
public class PersonnelDegree
{
public int PersonnelId { get; set; }
public int DegreeId { get; set; }
public virtual Personnel Personnel { get; set; }
public virtual Degree Degree { get; set; }
}
// Inside the OnModelCreating override
builder.Entity<Degree>().HasMany(x => x.Personnel);
builder.Entity<Personnel>().HasMany(x => x.Degrees);
builder.Entity<PersonnelDegree>()
.HasKey(x => new { x.DegreeId, x.PersonnelId });
builder.Entity<PersonnelDegree>()
.HasOne(x => x.Degree)
.WithMany(x => x.PersonnelDegrees)
.HasForeignKey(x => x.DegreeId);
builder.Entity<PersonnelDegree>()
.HasOne(x => x.Personnel)
.WithMany(x => x.PersonnelDegrees)
.HasForeignKey(x => x.PersonnelId);
现在,当我运行 dotnet ef migration add 人员时;我明白了……
migrationBuilder.CreateTable(
name: "Role",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySQL:AutoIncrement", true),
ApplicationId = table.Column<int>(nullable: true),
Name = table.Column<string>(maxLength: 100, nullable: true),
PersonnelId = table.Column<int>(nullable: true) // Where is this coming from?
},
constraints: table =>
{
table.PrimaryKey("PK_Role", x => x.Id);
table.ForeignKey(
name: "FK_Role_Application_ApplicationId",
column: x => x.ApplicationId,
principalTable: "Application",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
请注意,表定义中的 Role 包含 PersonnelId 列。而 Personnel 表有 RoleId?谁能告诉我这是怎么回事?
【问题讨论】:
-
那是 EF6 的,它不适用于 EF Core
-
这很奇怪,因为我只是在我的项目中使用它。
-
那么
Personnel和Role之间的关联呢?Degree和PersonnelDegree与此无关。
标签: mysql asp.net-core entity-framework-core .net-core