首页 > 解决方案 > 实体框架核心自动增量不起作用

问题描述

我正在使用 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)。我不知道如何解决这个问题。有人可以帮我吗?

标签: asp.net-coreentity-framework-coreauto-incrementfluent

解决方案


尝试使用 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");
            });

结果是这样的:

在此处输入图像描述


推荐阅读