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

ε祈祈猫儿з 提交于 2019-12-12 19:17:33

问题


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?

Update:

  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.)


回答1:


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

public class 201707132034165_MyAwesomeDbInitial : DbMigration
{
    #region <Methods>

    public override void Up()
    {
        CreateTable(
            "dbo.HasOverdrive",
            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
        SeedHasOverdrive(context);
    }

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

    #endregion
}


来源:https://stackoverflow.com/questions/33068501/entity-framework-6-code-first-int-identity-column-value-is-zero-for-first-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!