Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework auto increment with starting value

I have a entity as follows

[Table("ESS_POS_SE_VERSION")]
public class Version
{
    [Key, Column("DETAIL_ID"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Column("DETAIL_TITILE")]
    public string DetailKey { get; set; }

    [Column("DETAIL")]
    public string Detail { get; set; }
}

SO Id column is auto increment column, but I need to set its starting value to 1000. How can I do it?

like image 821
Madhawas Avatar asked Feb 12 '23 20:02

Madhawas


1 Answers

Consider creating a custom database initializer. It will be called each time your database is created or recreated.

For example:

public class MyInitializer : DropCreateDatabaseIfModelChanges<TContext> where TContext : DbContext
{
    protected override void Seed(TContext context)
    {
        context.Database.ExecuteSqlCommand("DBCC CHECKIDENT('MyTable', RESEED, 1000);");
    }
}

Then register it:

protected void Application_Start()
{
    Database.SetInitializer<MyContext>(new MyInitializer());
}
like image 145
nativehr Avatar answered Mar 20 '23 06:03

nativehr