Is There any way to RESEED a LocalDB Table using EF?
I'd prefer not to use this SQL Command :
DBCC CHECKIDENT('TableName', RESEED, 0)
FYI : I'm using EF 6.1.
Thanks alot.
You can reseed indentity values, that is, to have identity values reset or start at new predefined value by using DBCC CHECKIDENT. For example, a table named nwdsTable can be reseeded with the indentity column to 3.
The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.
Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…
I assume you're trying to reset the primary key on the table? If so, no there is no way in EF to do this.
As you stated, you would have to use a SQL command such as:
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT('TableName', RESEED, 0)")
But I have to ask why you're trying to do this anyway? It shouldn't matter to you what value is in your primary key field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With