0

Im new in ASP.NET Core and EF. When i try to add ApplicationUser as a foreign key, EF will create a new table "ApplicationUser" but i want to relate this field with the Identity Framework Table "ASPNetUser".

What do i wrong?

This is my code:

    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 created the migration like this:

  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");

But my Users will be created in this table:

But my Users will be created in this table:

Léon Zimmermann
  • 123
  • 1
  • 14

3 Answers3

0

It looks like you want to use the [Table("AspNetUsers")] annotation.

See here: How to Specify Entity Framework Core Table Mapping?

Important Caveat: Since it looks like you're trying to re-use an existing table, you will need to create a migration but then modify it in order to prevent Entity Framework from trying to recreate the table.

Slava Knyazev
  • 5,377
  • 1
  • 22
  • 43
0

So i figured it out... i forgot the Releationship in the DBContext... After i added this, my Migration works as expeted and added ApplicationUser as foreign Key in my Post Model:

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);
}
Léon Zimmermann
  • 123
  • 1
  • 14
0

[Key] attribute is used to create primary key. It being assigned to Creator property caused EF to create separate column

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; }
}
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40