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 update-database command is initially used to seed the database with data:

discovered that, instead of the anticipated one, all int Id columns started with zero (0). (1).

For each entity/table, add the following two lines of code to the top of the Seed function in Configuration. cs:

[Note: Due to foreign key constraints, I started with the ancestral chain and eliminated all rows in descendent tables.]

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

I then ran "update-database" again, and all of the int Id columns now had a value of 1. (1).

All of the int Id columns begin with zero if I dump the database, run/add the initial migration, and then run update-database (0).

As though theDBCC CHECKIDENT ('Widgets', RESEED, 0) The first time the Seed technique is used, no SQL statements are run.

Additionally, no matter how many times the update-database command is executed, the first time a row is added to an empty table, the Id will be zero. This applies to both entities and tables that lack seed data (0).

Is it possible to set the initial seed value for the int Identity columns in the override?OnModelCreating manner ofIdentityModels.cs ?


  1. Changed: DBCC CHECKIDENT ('Table', RESEED, 0) to DBCC CHECKIDENT ('Table', RESEED, 1) the configuration.cs file.

  2. The database was removed.

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

  4. Executing update-database the database to be reset. (No matter how many times you run, the result is that all int Ids begin with 2).update-database .)

10/11/2015 8:45:43 PM

Popular Answer

Your concrete migration class should "somewhat" resemble the examples below:

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