C# 如果EF正在创建新表,则无法将ApplicationUser设置为外键

C# 如果EF正在创建新表,则无法将ApplicationUser设置为外键,c#,.net,entity-framework,asp.net-identity,C#,.net,Entity Framework,Asp.net Identity,我是ASP.NET核心和EF的新手。 当我尝试将ApplicationUser添加为外键时,EF将创建一个新表“ApplicationUser”,但我想将此字段与Identity Framework表“ASPNetUser”关联起来 我错了什么 这是我的代码: public class Posts { public int Id { get; set; } public string Content { get; set; } public DateTime Cr

我是ASP.NET核心和EF的新手。 当我尝试将ApplicationUser添加为外键时,EF将创建一个新表“ApplicationUser”,但我想将此字段与Identity Framework表“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");
migrationBuilder.CreateTable(
名称:“应用程序用户”,
列:表=>new
{
Id=table.Column(可空:false),
用户名=table.Column(可空:true),
NormalizedUserName=table.Column(可空:true),
Email=table.Column(可空:true),
NormalizedEmail=table.Column(可空:true),
emailconfirm=table.Column(可空:false),
PasswordHash=table.Column(可空:true),
SecurityStamp=table.Column(可空:true),
ConcurrencyStamp=table.Column(可空:true),
PhoneNumber=table.Column(可空:true),
PhoneNumberConfiged=table.Column(可空:false),
TwoFactorEnabled=table.Column(可空:false),
LockoutEnd=table.Column(可空:true),
LockoutEnabled=table.Column(可空:false),
AccessFailedCount=table.Column(可空:false),
FirstName=table.Column(类型:“nvarchar(150)”,可为null:true),
LastName=table.Column(类型:“nvarchar(150)”,可为null:true),
Birthdate=table.Column(类型:“DateTime2”,可为空:false),
EntryDate=table.Column(类型:“DateTime2”,可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_ApplicationUser”,x=>x.Id);
});
migrationBuilder.CreateTable(
名称:“职位”,
列:表=>new
{
Id=table.Column(可空:false)
.Annotation(“SqlServer:Identity”、“1,1”),
Content=table.Column(可空:true),
Created=table.Column(可空:false),
CreatorId1=表.Column(可空:true),
CreatorId=table.Column(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_Posts”,x=>x.Id);
表1.外键(
名称:“FK_发布应用程序用户创建者1”,
列:x=>x.CreatorId1,
原则性:“应用程序用户”,
主栏:“Id”,
onDelete:referentialiction.Restrict);
});
migrationBuilder.CreateIndex(
名称:“IX_Posts_CreatorId1”,
表:“员额”,
栏目:“创建者1”);
但我的用户将在此表中创建:


看起来您想要使用
[表(“AspNetUsers”)]
注释

请看这里:


重要提示:由于看起来您正在尝试重新使用现有表,因此您需要创建一个迁移,然后对其进行修改,以防止Entity Framework尝试重新创建表

,因此我找到了解决方法。。。我忘记了DBContext中的关系。。。 添加此项后,我的迁移将以快捷方式进行,并将ApplicationUser作为外键添加到我的Post模型中:

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);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasOne(p=>p.Creator)
.有许多(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; }
}

ApplicationUser类是否在DbContext中?如果正确使用属性,则不需要在db context中建立关系,反之亦然