entity framework7身份种子
我们在EF7中使用Code First,我想添加一个具有Identity Seed的列,该另一个值为1。
目前,我们可以在迁移过程中通过EntityTypeBuilder将其设置为自动增量:
entityBuilder.Property(e => e.PropertyName).ValueGeneratedOnAdd();
但是我无法找到如何更改身份种子。 它是否仍然需要像其他版本的EF一样更新? 例如,编写一些自定义的SQL并在迁移期间运行它?
如何在entity framework代码中首先为几个表生成标识种子值
如何使用SQL Compact 4首先使用Entity Framework 4代码在ID列上设置Identity种子?
在EF7中,似乎没有SqlServerMigrationSqlGenerator > override Generate(AltherTableOperation alterTableOperation)
代码SqlServerMigrationSqlGenerator > override Generate(AltherTableOperation alterTableOperation)
?
在Entity Framework Core中使用Up
方法中的Sql
命令:
重要部分:
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(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"); } } }
是的,您必须编写所需的SQL语句来设置种子,然后在迁移中使用Sql方法。