Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何首先在代码中设置标识种子值?_C#_Entity Framework_Entity Framework Core - Fatal编程技术网

C# 如何首先在代码中设置标识种子值?

C# 如何首先在代码中设置标识种子值?,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我们首先使用EF core的代码,我想添加一个列,该列的标识种子从1以外的另一个值开始 目前,我们可以在迁移过程中通过EntityTypeBuilder将其设置为自动增量,方法是: entityBuilder.Property(e=>e.PropertyName.ValueGeneratedAndd(); 但是,我无法找到如何更改身份种子。它还需要像其他版本的EF一样更新吗?e、 g.编写一些自定义sql并在迁移过程中运行 在EF core中,似乎没有用于SqlServerMigratio

我们首先使用EF core的代码,我想添加一个列,该列的标识种子从1以外的另一个值开始

目前,我们可以在迁移过程中通过
EntityTypeBuilder
将其设置为自动增量,方法是:

entityBuilder.Property(e=>e.PropertyName.ValueGeneratedAndd();
但是,我无法找到如何更改身份种子。它还需要像其他版本的EF一样更新吗?e、 g.编写一些自定义sql并在迁移过程中运行


在EF core中,似乎没有用于
SqlServerMigrationSqlGenerator>重写生成(AlterTableOperation AlterTableOperation)

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

在实体框架核心中,使用
Up
方法中的
Sql
命令:

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

使用Microsoft.EntityFrameworkCore.Metadata;
使用Microsoft.EntityFrameworkCore.Migrations;
使用制度;
命名空间PaymentService.Migrations
{
公共部分类首字母:迁移
{
受保护的覆盖作废(MigrationBuilder MigrationBuilder)
{
migrationBuilder.CreateTable(
名称:“付款”,
列:表=>new
{
Id=table.Column(可空:false)
.Annotation(“SqlServer:ValueGenerationStrategy”,SqlServerValueGenerationStrategy.IdentityColumn)
},
约束:表=>
{
表.PrimaryKey(“PK_支付”,x=>x.Id);
});
//下面的代码用于设定标识的种子
Sql(“DBCC CHECKIDENT('Payment',RESEED,1000000)”;
}
受保护的覆盖无效关闭(MigrationBuilder MigrationBuilder)
{
migrationBuilder.DropTable(名称:“付款”);
}
}
}

更新2020

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

builder.Property(prop => prop.Id)
            .UseIdentityColumn(10000000, 1);
根据官方文件:

UseIdentityColumn将密钥属性配置为在以SQL Server为目标时使用SQL Server标识功能为新实体生成值。此方法将属性设置为OnAdd


谢谢。你知道这方面的任何文件吗?
builder.Property(prop => prop.Id)
            .UseIdentityColumn(10000000, 1);