【发布时间】:2020-11-06 21:25:12
【问题描述】:
我正在使用 Microsoft 的 ASP.NET Core MVC 模板。我在项目资源管理器(请参阅Printscreen: Project Explorer)中找到了 Data --> Migrations 文件“00000000000000_CreateIdentitySchemafil.cs”和“ApplicationDbContextModelSnapshot.cs”
我在“00000000000000_CreateIdentitySchemafile”文件中添加了以下代码:
migrationBuilder.CreateTable(
name: "Flights",
columns: table => new
{
Id = table.Column<int>(nullable: false),
StartDate = table.Column<DateTime>(nullable: true),
EndDate = table.Column<DateTime>(nullable: true),
UserId = table.Column<string>(maxLength: 450, nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_Flights", x => new { x.Id });
table.ForeignKey(
name: "FK_Flights_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
以及“ApplicationDbContextModelSnapshot”文件的以下代码:
modelBuilder.Entity("Flights", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("StartDate");
b.Property<DateTime>("EndDate");
b.Property<string>("UserId");
b.HasKey("Id");
b.ToTable("Flights");
});
modelBuilder.Entity("Flights", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
我可以成功创建表并将列 ID 声明为 PK(请参阅 Printscreen: DB)
不幸的是,当我想在这个表中插入一条记录时,它失败了,因为 Id 为空。
在 SQL-Server-Object-Explorer 中,我看到身份属性(种子、增量等)为 false 和 0(请参阅 Printscreen: ID-Property)。 我不知道如何解决这个问题。有人可以帮我吗?
【问题讨论】:
标签: asp.net-core entity-framework-core auto-increment fluent