Ef Code First - How to Set Identity Seed

EF Code First - how to set identity seed?

If you are using SQL Server you must create custom database initializer and manually execute DBCC CHECKIDENT ('TableName', RESEED, NewSeedValue). For creating and using custom initializer with custom SQL commands check this answer.

How to set Identity Seed value in code-first?

In Entity Framework Core Use Sql Command in Up method:

Important Part: 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");
}
}
}

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

You could try using this:

    internal class DefaultMigrationSqlGenerator : SqlServerMigrationSqlGenerator
{
protected override void Generate(AlterTableOperation alterTableOperation)
{
base.Generate(alterTableOperation);
// If the tables you want to reseed have an Id primary key...
if (alterTableOperation.Columns.Any(c => c.Name == "Id"))
{
string sqlSeedReset = string.Format("DBCC CHECKIDENT ({0}, RESEED, 1000) ", alterTableOperation.Name.Replace("dbo.", ""));

base.Generate(new SqlOperation(sqlSeedReset));
}
}
}

You can use a multitude of different options instead of AlterTableOperation, add a column, rename a column etc. to trigger this to run on every table. Unfortunately, you will have to do something that updates every table to be able to inject your own action on each of them.

The alternative is to script it in SQL Server - it's not like it would be a daily or weekly occurrence. Iterate through the tables, resetting the seed on each. But if you need something to happen on all tables regularly, and not in SQL Server, then I think the above is the way to go.

Entity Framework Code First: Initialize Identity Column with Seed and Increment Values

you may be able to add Seed for the Identity column in the following way

Consider the following Student class

public class Student
{
[Key, Column("Id")]
public int Id { get; set; }
public string Name { get; set; }
}

Now on it's Migration method add the SQL command DBCC CHECKIDENT to set the Seed value of 100

public override void Up()
{
CreateTable(
"dbo.Students",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(),
})
.PrimaryKey(t => t.Id);

Sql("DBCC CHECKIDENT ('dbo.Students', RESEED, 100);");
}

public override void Down()
{
DropTable("dbo.Students");
}

For changing the identity increment value, please see this to get idea.

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

I found the answer here:

context.Database
.ExecuteSqlCommand("ALTER TABLE Members ALTER COLUMN Id IDENTITY (10000,1)");

How to set starting value for an Identity primary key in Entity Framework code first?

It's not possible using ef annotations,but you can execute sql in migration UP method

Sql("DBCC CHECKIDENT ('Appointment', RESEED, 1000)");


Related Topics



Leave a reply



Submit