dataannotations 在主键上设置身份种子值,首先使用代码

Joe*_*Joe 4 data-annotations ef-code-first asp.net-mvc-3

ASP.NET MVC3 代码第一个项目。

在我的类定义中,如何设置 Identity 种子值。

  public class Account
  {
    [Key]   
    public int Id { get; set; }
Run Code Online (Sandbox Code Playgroud)

将 Identity 种子设置为 1000000 的语法是什么?

谢谢

Joe*_*Joe 5

谢谢克雷格,查看/sf/answers/418225951/后,这非常简单。

创建一个初始化器

public class MyInitializer : DropCreateDatabaseIfModelChanges<MyContext> 
{   
  protected override void Seed(MyContext context) 
  {   
    context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Account', RESEED, 1000000)");
  }  
}
Run Code Online (Sandbox Code Playgroud)

然后从 Global.asax.cs 的 Application_Start 部分调用

protected void Application_Start()
{
  Database.SetInitializer(new MyInitializer());
  AreaRegistration.RegisterAllAreas();

  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)