starrr
starrr

Reputation: 1023

The value of primary key doesn't change

I'm using visual studio 2010 an working on asp.net mvc3 project and sql server 2008. I have a table that the primary key of this table is int data type. I set Identity Specificatin "yes", Is Identity "yes", Identity Increment and Identity Seed "1". everything is OK when table is empty and data can be saved easily, But when I close visual studio, open it again and want to save data a break occured that shows the value of primary key is "1". (I deleted the data of database once, after this deletion the primary key of first row is 5 and other rows 6,7,...). What's the solution for this problem? your answer will be so helpfull. Thanks

Upvotes: 0

Views: 450

Answers (2)

gbn
gbn

Reputation: 432210

There is no solution for *automatic gap removal": identity columns always have gaps for good reasons. For example, the deleted row is probably in some history or audit table and you don't wan to reuse it.

However, you can reset the column by using DBCC CHECKIDENT

Note the gotchas here: SQL server identity column values start at 0 instead of 1

Upvotes: 2

juankysmith
juankysmith

Reputation: 12448

Using Sequence type for the Primary Key may help...

Upvotes: 1

Related Questions