Entity Framework auto increment with starting value

后端 未结 3 409
刺人心
刺人心 2020-12-17 05:07

I have a entity as follows

[Table(\"ESS_POS_SE_VERSION\")]
public class Version
{
    [Key, Column(\"DETAIL_ID\"), DatabaseGenerated(DatabaseGeneratedOption.         


        
3条回答
  •  隐瞒了意图╮
    2020-12-17 05:21

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

    For example:

    public class MyInitializer : DropCreateDatabaseIfModelChanges 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(new MyInitializer());
    }
    

提交回复
热议问题