Entity framework how to reset auto_increment code example

Example: Entity framework how to reset auto_increment

private static ContextName getContext()
{
	var contextOptions = new DbContextOptionsBuilder<ContextName>()
		.UseSqlServer("ConnectionString")
        .Options;
    return new ContextName(contextOptions);
}

//This is ran somewhere, I run mine in Main in its own seeder program
public void SeedData()
{
  	var context = getContext();
 	context.Database.EnsureCreated(); 
            
 	context.DbSet.RemoveRange(context.DbSet.ToList());
 	context.Database.ExecuteSqlRaw("DBCC CHECKIDENT('table.path', RESEED, 0)"); //Reset AUTO_INCREMENT

 	context.PricePageDatas.AddRange(_someCollection); // This comes from a modified list of what you want to poopulate database with
 	context.SaveChanges();
}

Tags:

Misc Example