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: