繁体   English   中英

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

[英]EntityFramework Code-first migration ignoring [Key] and forcing composite key

我正在尝试使用[Key]创建一个带有 Guid 主 [Key] 的 object,但 EntityFramework 不断强制创建复合键。

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 class,并且 EntityFramework 尊重那里的[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);
                });

偶当我把go进迁移改

table.PrimaryKey("PK_Challenges", x => new { x.ChallengerId, x.ChallengeeId });

table.PrimaryKey("PK_Challenges", x => x.Id);

EntityFramework 似乎在苦苦挣扎,因为它在 SQL 数据库中使用复合键创建了挑战。

我正在使用 .NET 6。

您的播放器 model 中可能缺少 [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 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM