dataannotations与code主键设置身份的种子值第一 [英] dataannotations set identity seed value on Primary Key with code first

查看:188
本文介绍了dataannotations与code主键设置身份的种子值第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC3 code第一个项目。

ASP.NET MVC3 code first project.

在我的类定义如何设置标识种子值。

In my class definition how do I set the Identity seed value.

  public class Account
  {
    [Key]   
    public int Id { get; set; }

什么是设置标识种子语法1000000?

What would be the syntax to set the Identity seed to 1000000?

感谢您

推荐答案

感谢克雷格,看后 http://stackoverflow.com / A /九十六万八千三百零一分之五百九十七万四千六百五十六是pretty简单。

Thanks Craig, after looking at http://stackoverflow.com/a/5974656/968301 it was pretty simple.

创建初始化器

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

然后从Global.asax.cs中的部分的Application_Start叫

Then call from the Application_Start section of the Global.asax.cs

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

  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);
}

这篇关于dataannotations与code主键设置身份的种子值第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆