【发布时间】:2020-08-04 11:08:04
【问题描述】:
问题在于 EF 核心正在为多对多关系创建额外的外键列,即使我已经指定了外键是什么。
我有以下实体模型,需要多对多关系
ParticipantModel.cs
public class ParticipantModel
{
public int Id { get; set; }
public HashSet<ThreadsParticipants> ThreadsParticipants { get; set; } = new HashSet<ThreadsParticipants>();
}
ThreadModel.cs
public class ThreadModel
{
public int Id { get; private set; }
public HashSet<ThreadsParticipants> ThreadsParticipants { get; set; } = new HashSet<ThreadsParticipants>();
}
ThreadsParticipants.cs(加入表)
public class ThreadsParticipants
{
public int ThreadModelId { get; private set; }
public ThreadModel ThreadModel { get; private set; } = default!;
public int ParticipantModelId { get; private set; }
public ParticipantModel ParticipantModel { get; private set; } = default!;
}
以及以下配置EntityTypeConfigurations:
public void Configure(EntityTypeBuilder<ParticipantModel> builder)
{
builder.HasKey(p => p.Id);
}
public void Configure(EntityTypeBuilder<ThreadModel> builder)
{
builder.HasKey(t => t.Id);
}
public void Configure(EntityTypeBuilder<ThreadsParticipants> builder)
{
builder
.HasKey(tp => new { tp.ThreadModelId, tp.ParticipantModelId });
builder
.HasOne(tp => tp.ThreadModel)
.WithMany()
.HasForeignKey(tp => tp.ThreadModelId)
.IsRequired();
builder
.HasOne(tp => tp.ParticipantModel)
.WithMany()
.HasForeignKey(tp => tp.ParticipantModelId)
.IsRequired();
}
当运行add-migration 时,会生成创建 ThreadsParticipants 表的块:
migrationBuilder.CreateTable(
name: "MessageThreadsParticipants",
columns: table => new
{
ThreadModelId = table.Column<int>(nullable: false),
ParticipantModelId = table.Column<int>(nullable: false),
ParticipantModelId1 = table.Column<int>(nullable: true),
ThreadModelId1 = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_MessageThreadsParticipants", x => new { x.ThreadModelId, x.ParticipantModelId });
table.ForeignKey(
name: "FK_MessageThreadsParticipants_MessageParticipants_ParticipantModelId",
column: x => x.ParticipantModelId,
principalTable: "MessageParticipants",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_MessageThreadsParticipants_MessageParticipants_ParticipantModelId1",
column: x => x.ParticipantModelId1,
principalTable: "MessageParticipants",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_MessageThreadsParticipants_MessageThreads_ThreadModelId",
column: x => x.ThreadModelId,
principalTable: "MessageThreads",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_MessageThreadsParticipants_MessageThreads_ThreadModelId1",
column: x => x.ThreadModelId1,
principalTable: "MessageThreads",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
当我明确指定我的外键是什么时,为什么会生成这些额外的外键属性?
【问题讨论】:
-
只是为了帮助您沟通问题:如果您遵循 EF 文档标准,这不是多对多关系。这是两个单独的一对多关系(您在功能上将其用作多对多,但 EF 并不关心您的意图)。在 EF 的上下文中,多对多是指您不指定连接表的情况,EF 为您制作并隐藏它。请注意,EF Core 目前不支持多对多(原来的 EF 确实支持)。
-
感谢您的澄清
标签: c# .net-core entity-framework-core ef-fluent-api