4

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

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

Thank you

Joe
  • 4,143
  • 8
  • 37
  • 65
  • Do you want to start the seed at 1000000 or do you want to set the Id for a specific entity? – Kane Jun 29 '12 at 02:52
  • http://stackoverflow.com/a/5974656/968301 – Craig Curtis Jun 29 '12 at 03:20
  • Kane I want to start the seed at 1000000. Not sure I follow the second question. I want the Primary Key ID in the Account table to be seeded with 1000000 when it is created and increment by 1 from there. – Joe Jun 29 '12 at 06:48
  • Craig Not simple huh? I'll try and take a closer look at that link and its links. I was hoping for something with dataannotations or fluent API. – Joe Jun 29 '12 at 06:50
  • For EF Core Identity Seed notice to [This question](https://stackoverflow.com/questions/34927619/entity-framework-7-identity-seed/48595581#48595581) – Sina Lotfi Feb 03 '18 at 11:10

1 Answers1

5

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

Create an intializer

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

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);
}
Community
  • 1
  • 1
Joe
  • 4,143
  • 8
  • 37
  • 65