Answers for "Entity framework how to reset auto_increment"

C#
0

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();
}
Posted by: Guest on January-21-2021

Code answers related to "Entity framework how to reset auto_increment"

C# Answers by Framework

Browse Popular Code Answers by Language