【发布时间】:2022-01-12 02:28:44
【问题描述】:
我正在尝试使用 [Key] 创建具有 Guid 主键的对象,但 Entity Framework 不断强制创建复合键。
public class Challenge
{
[Key]
public Guid Id { get; set; }
public Guid ChallengerId { get; set; }
public Guid ChallengeeId { get; set; }
[ForeignKey("ChallengerId")]
public virtual Player Challenger { get; set; }
[ForeignKey("ChallengeeId")]
public virtual Player Challengee { get; set; }
public DateTime Initiated { get; set; }
}
及其迁移...
migrationBuilder.CreateTable(
name: "Challenges",
columns: table => new
{
ChallengerId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
ChallengeeId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Initiated = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Challenges", x => new { x.ChallengerId, x.ChallengeeId });
table.ForeignKey(
name: "FK_Challenges_Players_ChallengeeId",
column: x => x.ChallengeeId,
principalTable: "Players",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Challenges_Players_ChallengerId",
column: x => x.ChallengerId,
principalTable: "Players",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
请注意,我使用相同的方法创建了 Player 类,并且 Entity Framework 尊重那里的 [Key] 属性。
public class Player
{
[Key]
public Guid Id { get; set; }
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual IdentityUser User { get; set; }
public virtual ICollection<Game> Games { get; set; }
[InverseProperty("Challenger")]
public virtual ICollection<Challenge> OutgoingChallenges { get; set; }
[InverseProperty("Challengee")]
public virtual ICollection<Challenge> IncomingChallenges { get; set; }
}
migrationBuilder.CreateTable(
name: "Players",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
UserId = table.Column<string>(type: "nvarchar(450)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Players", x => x.Id);
table.ForeignKey(
name: "FK_Players_IdentityUser_UserId",
column: x => x.UserId,
principalTable: "IdentityUser",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
即使我进入迁移和改变
table.PrimaryKey("PK_Challenges", x => new { x.ChallengerId, x.ChallengeeId });
到
table.PrimaryKey("PK_Challenges", x => x.Id);
Entity Framework 似乎在苦苦挣扎,因为它使用复合键在 SQL 数据库中创建 Challenges。
我正在使用 .NET 6。
【问题讨论】:
-
你有流畅的api吗?
-
我不确定?这是一个相当新的/稀疏的项目。为什么这会影响实体构建器?
标签: c# .net entity-framework entity-framework-core entity-framework-migrations