数据注释首先在主键上设置标识种子值 [英] dataannotations set identity seed value on Primary Key with code first

查看:130
本文介绍了数据注释首先在主键上设置标识种子值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

谢谢

推荐答案

感谢Craig,看着https://stackoverflow.com/a/5974656/968301 很简单。

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

创建一个intializer

Create an intializer

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

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

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);
}

这篇关于数据注释首先在主键上设置标识种子值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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