AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Needed to reset Student ID seed value when re-initializing data

Open Millardh77 opened this issue 8 years ago • 3 comments

Just something that might help others...I had to reset the student ID after I had added a row in testing and then later attempting to invoke the DbInitializer. When I allowed it to attempt to seed, I wasn't getting any data in the Enrollment table and it was throwing an error because the StudentID foreign key wasn't in sync with the identity field ID of the Student table. I just ran: DBCC CHECKIDENT ('Student', RESEED, 0). and it fixed the issue. I wasn't sure if I just missed a step or something. :)


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Millardh77 avatar Sep 12 '18 15:09 Millardh77

@Millardh77 thanks for the tip. Changing the PK is generally not recommended and very problematic.

Rick-Anderson avatar Sep 12 '18 17:09 Rick-Anderson

Not trying to pick, but in this situation, in order to get the data populated to all of the tables as expected, either the primary key has to be reset or each of the records in the enrolment table would have to be changed to reflect the new student ids to satisfy the FK constraint. I know this is a tutorial with demo data and you wouldn't want to do that in a production environment, but I was only trying to get it to work and that was a simple solution.

Millardh77 avatar Sep 12 '18 18:09 Millardh77

I have to agree with @Millardh77. This is not an issue of changing a primary key. This was a demo, where things are supposed to work. An inexperienced developer would follow along and, as per instructions in the tutorial, add and delete a record in the Student table as part of testing the scaffolded Student Pages. In deleting the Student (or Students) they inserted, those StudentID's of 1 (or more) are no longer available, since the Student table is using IDENTITY INSERT - the values, once deleted, are no longer available. When the student copies the code for inserting Enrollment records, the code is using StudentID's that will cause a referential constraint failure because the StudentID in the Enrollment table is a FOREIGN KEY. Perhaps totally unaware that the Student table, once a Student record is deleted, will never allow a StudentID of "1", will insert data into the Enrollment table, as instructed, and get this error. without the experience to see that the Enrollment records inserted have to start at the NEXT StudentID, not at "1", as the instructions tell us. A novice would throw up their hands in frustration. @Millardh77's solution is also correct, DBCC CHECKIDENT ('Student', RESEED, 0), though it should be added to the tutorial in a way a novice can execute [I had to get to SQL Server Management Studio and figure out how to connect to the (localdb)\mssqllocaldb instance]. Otherwise, users who add and delete more than one record while testing the scaffolded Students forms will have to have different StudentID values inserted into the Enrollment table for the code to succeed.

Loves2Code avatar Mar 26 '19 17:03 Loves2Code