【问题标题】:Enitity Framework Core auto increment doesn't work实体框架核心自动增量不起作用
【发布时间】: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


    【解决方案1】:

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

    结果如下:

    【讨论】:

    • 非常感谢。不幸的是,它不起作用。作为解决方法,我在 sql management studio 中手动设置了这些属性(种子、增量等)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多