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
When Is Optimization Premature
.Net Regex Matching $ with the End of the String and Not of Line, Even with Multiline Enabled
Is It Necessary to Explicitly Remove Event Handlers in C#
Best Way to Convert Callback-Based Async Method to Awaitable Task
How to Run a .Net 4.5 App on Xp
How to See What My Reactive Extensions Query Is Doing
When Is a C# Value/Object Copied and When Is Its Reference Copied
Reference Type Still Needs Pass by Ref
Conversion from List<T> to Array T[]
Implementing Hoey Shamos Algorithm with C#
Camera.Main Is Null When Performing Raycast
Token Based Authentication in ASP.NET Core (Refreshed)
What Do Programmers Mean When They Say, "Code Against an Interface, Not an Object."
What Is the Efficiency and Performance of Linq and Lambda Expression in .Net
How to Define a Method in Razor
How to Display the Displayattribute.Description Attribute Value