Entity Framework 代码优先迁移忽略 [Key] 并强制使用复合键

Entity Framework code-first migration ignoring [Key] and forcing composite key

我正在尝试使用 [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);
                });

请注意,我使用相同的方法创建了播放器 class,并且 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。

您的 Player 模型中是否缺少 [Key] 属性?

public class Player
{
    //Here should be a [Key] annotation
    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; }
}

我找到了罪魁祸首:AppDbContext.cs

modelBuilder.Entity<Challenge>()
                    .HasForeignKey(c => new { c.ChallengerId, c.ChallengeeId });