简体   繁体   中英

Entity Framework Code First: Initialize Identity Column with Seed and Increment Values

I have a fresh database and am looking to set the the seed and increment of the decimal identity column (example column below).

[Key, Column("id")]
public decimal id { get; set; }

Is it possible to set the seed AND the increment values? Would I have to modify the OnCreating() method?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM