如果没有 EF 正在创建新的 table,则无法将 ApplicationUser 设置为外键

Not able to set ApplicationUser as foreign key without EF is creating a new table

我是 ASP.NET Core 和 EF 的新成员。 当我尝试将 ApplicationUser 添加为外键时,EF 将创建一个新的 table“ApplicationUser”,但我想将该字段与身份框架相关联 Table“ASPNetUser”。

我错了什么?

这是我的代码:

    public class Posts
{
    public int Id { get; set; }
    public string Content { get; set; }
    public DateTime Created { get; set; }
    [Key]
    public int CreatorId { get; set; }

    [ForeignKey("CreatorId")]
    public ApplicationUser Creator { get; set; }
}

EF 是这样创建迁移的:

  migrationBuilder.CreateTable(
            name: "ApplicationUser",
            columns: table => new
            {
                Id = table.Column<string>(nullable: false),
                UserName = table.Column<string>(nullable: true),
                NormalizedUserName = table.Column<string>(nullable: true),
                Email = table.Column<string>(nullable: true),
                NormalizedEmail = table.Column<string>(nullable: true),
                EmailConfirmed = table.Column<bool>(nullable: false),
                PasswordHash = table.Column<string>(nullable: true),
                SecurityStamp = table.Column<string>(nullable: true),
                ConcurrencyStamp = table.Column<string>(nullable: true),
                PhoneNumber = table.Column<string>(nullable: true),
                PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                TwoFactorEnabled = table.Column<bool>(nullable: false),
                LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
                LockoutEnabled = table.Column<bool>(nullable: false),
                AccessFailedCount = table.Column<int>(nullable: false),
                FirstName = table.Column<string>(type: "nvarchar(150)", nullable: true),
                LastName = table.Column<string>(type: "nvarchar(150)", nullable: true),
                Birthdate = table.Column<DateTime>(type: "DateTime2", nullable: false),
                EntryDate = table.Column<DateTime>(type: "DateTime2", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_ApplicationUser", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "Posts",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                Content = table.Column<string>(nullable: true),
                Created = table.Column<DateTime>(nullable: false),
                CreatorId1 = table.Column<string>(nullable: true),
                CreatorId = table.Column<int>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Posts", x => x.Id);
                table.ForeignKey(
                    name: "FK_Posts_ApplicationUser_CreatorId1",
                    column: x => x.CreatorId1,
                    principalTable: "ApplicationUser",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Posts_CreatorId1",
            table: "Posts",
            column: "CreatorId1");

但我的用户将在此 table:

中创建

您似乎想使用 [Table("AspNetUsers")] 注释。

看这里:

重要警告:因为看起来您正在尝试 re-use 现有的 table,您将需要创建一个迁移,然后 修改它以防止Entity Framework 尝试重新创建 table

所以我想通了...我忘记了 DBContext 中的关系... 在我添加这个之后,我的迁移按预期工作并在我的 Post 模型中添加了 ApplicationUser 作为外键:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Posts>()
        .HasOne(p => p.Creator)
        .WithMany(p => p.Posts)
        .HasForeignKey(p => p.CreatorId)
        .OnDelete(DeleteBehavior.Cascade);
}

[Key] 属性用于创建主键。它被分配给 Creator 属性 导致 EF 创建单独的列

public class Posts
{
    [Key]
    public int Id { get; set; }
    public string Content { get; set; }
    public DateTime Created { get; set; }
    
    [ForeignKey("Creator")]
    public int CreatorId { get; set; }

    [ForeignKey("CreatorId")]
    public ApplicationUser Creator { get; set; }
}