简体   繁体   English

实体框架6代码First int Identity第一列的值为零

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

After the first execution of the update-database command to populate the database with seed data: 在第一次执行update-database命令以用种子数据填充数据库之后:

Found that all int Id columns started with zero (0) rather than the expected one (1). 发现所有int Id列均以零(0)开头,而不是预期的一(1)。

Added the following 2 lines of code for each entity/table to the top of the Seed method in Configuration.cs: 在Configuration.cs的Seed方法顶部,为每个实体/表添加了以下两行代码:

[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). 然后,我重新运行了update-database,所有ind Id列均以一(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). 如果删除数据库,运行/添加初始迁移,然后运行update-database,则所有int Id列均以零(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. 就像第一次运行Seed方法时未执行DBCC CHECKIDENT ('Widgets', RESEED, 0) SQL语句一样。

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). 同样,如果没有实体/表的种子数据,则不管运行update-database命令多少次,第一次将一行添加到空表中时,Id均为零(0)。

Possibly, is there a way to specify the initial seed value for int Identity columns in the override OnModelCreating method of IdentityModels.cs ? 可能地,是有指定的倍率为INT标识列的初始种子值的方式OnModelCreating的方法IdentityModels.cs

Update: 更新:

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

  2. Deleted the database. 删除数据库。

  3. Executed: update-database -TargetMigration Initial (Result is all int Ids start with 1.) 执行: update-database -TargetMigration Initial (结果是所有从1开始的int Id。)

  4. Executing update-database to reset the database. 执行update-database以重置数据库。 (Result is all int Ids start with 2, no matter how many times you run update-database .) (结果是所有int Ids都以2开头,无论您运行update-database多少次。)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Entity Framework代码优先中将列更改为唯一身份 - How to alter column to unique identity in Entity Framework Code First 实体框架代码优先:使用种子和增量值初始化标识列 - Entity Framework Code First: Initialize Identity Column with Seed and Increment Values 使用MVC 5在Entity Framework 6 Code First中按int对列进行排序 - Sorting a column by an int in Entity Framework 6 Code First using MVC 5 使用现有数据库和实体框架的代码优先:当IDENTITY_INSERT设置为OFF时,无法为表中的标识列插入显式值 - Code First With Existing Database, Entity Framework: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF 实体框架代码优先不会将身份设置为是 - Entity Framework Code First will not set Identity to Yes 具有关系的实体框架6代码优先更新行 - Entity Framework 6 Code First Update Row with Relations 如何将主键标识列添加到实体框架代码首先生成的模型 - How do I add a Primary Key identity column to Entity Framework Code First generated Model 实体框架代码第一个TPH标识符列 - Entity Framework Code First TPH Discriminator Column 实体框架代码优先 - 列ID无效 - Entity Framework Code First - Invalid column ID 首先在实体框架代码中禁用列 - Disabling Column in Entity Framework Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM