Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to RESEED LocalDB Table using Entity Framework?

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.

like image 998
Mahdi Rashidi Avatar asked Oct 02 '14 07:10

Mahdi Rashidi


People also ask

What is reseeding in SQL Server?

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.

What is OnModelCreating in Entity Framework?

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.

How do I use code first in Entity Framework?

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…


1 Answers

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.

like image 158
Darren Lamb Avatar answered Sep 18 '22 12:09

Darren Lamb