简体   繁体   English

实体框架代码优先:使用种子和增量值初始化标识列

[英]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? 我是否需要修改OnCreating()方法?

you may be able to add Seed for the Identity column in the following way 您可以通过以下方式为“ Identity列添加“ Seed

Consider the following Student class 考虑以下Student

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 现在,在其Migration方法上,添加SQL命令DBCC CHECKIDENT以将Seed值设置为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. 有关更改身份增量值的信息,请参阅此内容

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

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