Entity Framework 6 Code First int Identity column value is zero for first row

c# ef-code-first entity-framework-6 sql-server


After the first execution of the update-database command to populate the database with seed data:

Found that all int Id columns started with zero (0) rather than the expected one (1).

Added the following 2 lines of code for each entity/table to the top of the Seed method in Configuration.cs:

[Note: Because of the foreign key constraints, I deleted all rows in descendent tables, and then worked my way up the ancestral chain.]

context.Database.ExecuteSqlCommand("delete from Widgets");
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Widgets', RESEED, 0)");

Then, I reran update-database, and all of the int Id columns started with one (1).

If I drop the database, run/add the initial migration and then run update-database, all of the int Id columns start with zero (0).

It is as if the DBCC CHECKIDENT ('Widgets', RESEED, 0) SQL statements are not being executed the first time that the Seed method is run.

Also, if there is no seed data for an entity/table, it does not matter how many times the update-database command is run, the first time a row is added to the empty table, the Id will be zero (0).

Possibly, is there a way to specify the initial seed value for int Identity columns in the override OnModelCreating method of IdentityModels.cs?


  1. Changed: DBCC CHECKIDENT ('Table', RESEED, 0) to DBCC CHECKIDENT ('Table', RESEED, 1) in Configuration.cs.

  2. Deleted the database.

  3. Executed: update-database -TargetMigration Initial (Result is all int Ids start with 1.)

  4. Executing update-database to reset the database. (Result is all int Ids start with 2, no matter how many times you run update-database.)

10/11/2015 8:45:43 PM

Popular Answer

Your concrete migration class should look "something" like the following:

public class 201707132034165_MyAwesomeDbInitial : DbMigration
    #region <Methods>

    public override void Up()
            c => new
                HasOverdriveId = c.Int(nullable: false, identity: true),
                HasOverdriveValue = c.String(nullable: false, maxLength: 5)
            .PrimaryKey(t => t.HasOverdriveId)
            .Index(t => t.HasOverdriveValue, unique: true, name: "UX_HasOverdrive_AlternateKey");

    // This should is called by your DbConfiguration class
    public void Seed(MyAwesomeDbContext context)
        // DO THIS FIRST !!!!!!!!!!!!!!!
        context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('HasOverdrive', RESEED, 0)");

        // LOOKUPS

    private void SeedHasOverdrive(MeasurementContractsDbContext context)
            m => m.Id,
            new HasOverdrive { HasOverdriveId = 0, HasOverdriveValue = "No" }, // 0 = FALSE 
            new HasOverdrive { HasOverdriveId = 1, HasOverdriveValue = "Yes" } // 1 = TRUE

7/13/2017 8:50:31 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow