, , OnModelCreating db, base.OnModelCreating(modelBuilder);.
, .
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
: , , . TL;DR , . , , , , , - , , , , , .
.net Identity. , , , . .
Todo. , "" "". .
public class Todo
{
public int Id { get; set; }
public string UserId { get; set; }
public ApplicationUser User { get; set; }
}
ApplicationDbContext DbSet. , ToDo DbSet
add-migration todo , todo. , , FK.
public partial class Todo : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Todos",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
UserId = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Todos", x => x.Id);
table.ForeignKey(
name: "FK_Todos_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Todos_UserId",
table: "Todos",
column: "UserId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Todos");
}
}
" ", , , FK . .
public async Task<IActionResult> About()
{
var user = await _userManager.GetUserAsync(HttpContext.User);
var todo = new Todo
{
User = user
};
_context.Todos.Add(todo);
await _context.SaveChangesAsync();
return View();
}
:

, Todo, - , -, ?