【问题标题】:How to set Identity Seed value in code-first?如何在代码优先中设置身份种子值?
【发布时间】:2021-08-02 18:17:20
【问题描述】:

我们将 Code First 与 EF-core 一起使用,我想添加一个列,该列的标识种子从 1 以外的另一个值开始。

目前我们可以在迁移期间通过EntityTypeBuilder 将其设置为自动递增:

entityBuilder.Property(e => e.PropertyName).ValueGeneratedOnAdd();

但是我不知道如何更改身份种子。它是否仍需要像其他版本的 EF 一样更新?例如编写一些自定义 sql 并在迁移期间运行它?

How to seed identity seed value in entity framework code first for several tables

How do I set Identity seed on an ID column using Entity Framework 4 code first with SQL Compact 4?

在 EF-core 中似乎没有 SqlServerMigrationSqlGenerator > override Generate(AlterTableOperation alterTableOperation) 的代码?

【问题讨论】:

    标签: c# entity-framework entity-framework-core


    【解决方案1】:

    2020 年更新

    在 EF Core 3.0 之后,您现在有了一个 UseIdentityColumn 扩展方法,可用于设置标识列的种子值和增量值。

    builder.Property(prop => prop.Id)
                .UseIdentityColumn(10000000, 1);
    

    根据官方文档:

    UseIdentityColumn 配置键属性以在面向 SQL Server 时使用 SQL Server IDENTITY 功能为新实体生成值。此方法将属性设置为 OnAdd。

    Link

    【讨论】:

      【解决方案2】:

      在 Entity Framework Core 中使用Sql 命令在Up 方法中:

      重要部分:migrationBuilder.Sql("DBCC CHECKIDENT ('Payment', RESEED, 1000000)");

      using Microsoft.EntityFrameworkCore.Metadata;
      using Microsoft.EntityFrameworkCore.Migrations;
      using System;
      
      namespace PaymentService.Migrations
      {
          public partial class Initial : Migration
          {
              protected override void Up(MigrationBuilder migrationBuilder)
              {
                  migrationBuilder.CreateTable(
                  name: "Payment",
                  columns: table => new
                  {
                      Id = table.Column<int>(nullable: false)
                                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
                  },
                  constraints: table =>
                  {
                      table.PrimaryKey("PK_Payment", x => x.Id);
                  });
      
                  // Below code is for seeding the identity
                  migrationBuilder.Sql("DBCC CHECKIDENT ('Payment', RESEED, 1000000)");
              }
      
              protected override void Down(MigrationBuilder migrationBuilder)
              {
                  migrationBuilder.DropTable(name: "Payment");
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        是的,你必须编写所需的SQL语句来设置种子,然后在迁移中使用Sql方法。

        【讨论】:

        • 谢谢。你知道这方面的任何文件吗?
        【解决方案4】:

        如果您使用的是 MySQL 而不是 SQL Server,请添加以下内容来代替第一个答案中的 "migrationBuilder.Sql("DBCC CHECKIDENT ('Payment', RESEED, 1000000)");"

        migrationBuilder.Sql("use db_name; ALTER TABLE Clients AUTO_INCREMENT = start_value;");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-07-28
          • 1970-01-01
          • 1970-01-01
          • 2012-05-08
          • 2017-08-25
          • 1970-01-01
          • 2019-02-21
          相关资源
          最近更新 更多