开发者

DBCC CheckIdent existing tables inserts + 1 - nunit tests are now unpredictable

Have a problem with DBCC CHECKIDENT. I clear out all db tables before my NUnit tests and I need to be able to add a new user by specifying the Id = 1. For this I need to delete all data in the db, then reseed the users table so the Id is predictable enough to be used in my Nunit tests.

When I run the following:

DBCC CHECKIDENT ('users', RESEED, 1)

I get a new user having an ID of 1 if the database is re-created at the beginning of the text fixture setup. However, if the database already exists, and the data is cleared, the next insert will have a user with an Id of 2!!

I need the Primary Key to be reset regardless if this is a fresh d开发者_Go百科atabase or pre-existing with just cleared out tables.

Can't believe this feature exhibits this behaviour, so annoying!


I clear out all db tables before my NUnit tests

That is your true problem. You'll never get it right, no matter how you do. Have your NUnit test setup create the database from scratch, using upgrade scripts.


Yeah, this is annoying. This behaves differently on a fresh b-tree. Solution: Insert a null row that is invalid. This statement will crash. But it will make your counter work because your b-tree is no longer empty.

Different solution: Do a truncate table which deletes the b-tree and gives you a fresh one. You need to drop all FKs before you do this.

Another one: Do the reseed, then check what the current identity value is. If it is wrong, reseed again. You can check the current value either by doing "DBCC CHECKIDENT (T, NORESEED)" or by executing a query: "select max(id) from T".

Another one: Script the whole database with SMO (.NET) and create a fresh one from script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜