asp.net-core - 实体框架核心自动增量不起作用
问题描述
我正在使用 Microsoft 的 ASP.NET Core MVC 模板。我 在 Data --> Migrations 文件“00000000000000_CreateIdentitySchemafil.cs”和“ApplicationDbContextModelSnapshot.cs”下的 Project Explorer 中找到了(参见Printscreen: Project Explorer )
我在“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)。我不知道如何解决这个问题。有人可以帮我吗?
解决方案
尝试使用 Annotation 方法来设置 Identity 列。根据您的代码,我在我这边添加了一个航班表,生成的代码如下:
migrationBuilder.CreateTable(
name: "Flights",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
StartDate = table.Column<DateTime>(nullable: false),
EndDate = table.Column<DateTime>(nullable: false),
UserId = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Flights", x => x.Id);
table.ForeignKey(
name: "FK_Flights_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Flights_UserId",
table: "Flights",
column: "UserId");
}
和
modelBuilder.Entity("EFCore.Models.Flights", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<DateTime>("EndDate")
.HasColumnType("datetime2");
b.Property<DateTime>("StartDate")
.HasColumnType("datetime2");
b.Property<string>("UserId")
.HasColumnType("nvarchar(450)");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Flightss");
});
结果是这样的:
推荐阅读
- mongodb - Mongoose 不在 docker 内连接
- reactjs - 更改完成长时间操作后返回的组件
- elasticsearch - ElasticSearch 中文档的位置
- python - 为 boto 脚本提供的 XML 格式不正确
- php - 如何在 PHP 中对相似键上的关联数组元素进行分组
- c++ - 在实现邻接列表时,一维向量是否会自动转换为二维向量?
- sql-server - 如何在不修改源文件的情况下在 BULK INSERT 期间添加其他列
- javascript - 递归过滤和删除对象数组中的项目
- php - 如何使用 php 在 geoserver 实现中从 mapfish 打印下载 pdf 文件?
- python - `register_forward_hook` 在使用多个 GPU 时出错