This should all work with SQLite. We should add that next review.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
I was having issues with DbInitializer, whereby Student and Course tables were populated, Enrollment was not.
After a crash course in debugging with breakpoints, I found the error I needed:
"System.Data.SqlClient.SqlException: The MERGE statement conflicted with the FOREIGN KEY constraint "FK_Enrollment_Student_StudentID"."
My database was initialised with the following Student data:
ID LastName FirstMidName EnrollmentDate
1074 Alexander Carson 2005-09-01 00:00:00.0000000
1075 Alonso Meredith 2002-09-01 00:00:00.0000000
1076 Anand Arturo 2003-09-01 00:00:00.0000000
1077 Barzdukas Gytis 2002-09-01 00:00:00.0000000
1078 Li Yan 2002-09-01 00:00:00.0000000
1079 Justice Peggy 2001-09-01 00:00:00.0000000
1080 Norman Laura 2003-09-01 00:00:00.0000000
1081 Olivetto Nino 2005-09-01 00:00:00.0000000
Lets look at the Enrollment values from the DBInitializer script:
var enrollments = new Enrollment[]
{
new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A},
new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C},
new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B},
new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B},
new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F},
new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F},
new Enrollment{StudentID=3,CourseID=1050},
new Enrollment{StudentID=4,CourseID=1050},
new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F},
new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C},
new Enrollment{StudentID=6,CourseID=1045},
new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A},
};
The Student IDs in my database do not match the StudentIDs for the Enrollment records in the DBInitializer scipt! My blundering around had incremented the Student identity keys and so would not allow the creation of the enrollment records.
Resolved by running the following SQL on my database:
USE [SchoolContext-2d213d3a-3291-4719-8720-180bc8848ee7]
delete from [dbo].Student
delete from [dbo].Course
delete from [dbo].Enrollment
DBCC CHECKIDENT ('[dbo].Student', reseed,0);
Hope this helps someone else :)
@jmsfnch thanks!
Most helpful comment
I was having issues with DbInitializer, whereby Student and Course tables were populated, Enrollment was not.
After a crash course in debugging with breakpoints, I found the error I needed:
"System.Data.SqlClient.SqlException: The MERGE statement conflicted with the FOREIGN KEY constraint "FK_Enrollment_Student_StudentID"."
My database was initialised with the following Student data:
ID LastName FirstMidName EnrollmentDate
1074 Alexander Carson 2005-09-01 00:00:00.0000000
1075 Alonso Meredith 2002-09-01 00:00:00.0000000
1076 Anand Arturo 2003-09-01 00:00:00.0000000
1077 Barzdukas Gytis 2002-09-01 00:00:00.0000000
1078 Li Yan 2002-09-01 00:00:00.0000000
1079 Justice Peggy 2001-09-01 00:00:00.0000000
1080 Norman Laura 2003-09-01 00:00:00.0000000
1081 Olivetto Nino 2005-09-01 00:00:00.0000000
Lets look at the Enrollment values from the DBInitializer script:
var enrollments = new Enrollment[]
{
new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A},
new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C},
new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B},
new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B},
new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F},
new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F},
new Enrollment{StudentID=3,CourseID=1050},
new Enrollment{StudentID=4,CourseID=1050},
new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F},
new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C},
new Enrollment{StudentID=6,CourseID=1045},
new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A},
};
The Student IDs in my database do not match the StudentIDs for the Enrollment records in the DBInitializer scipt! My blundering around had incremented the Student identity keys and so would not allow the creation of the enrollment records.
Resolved by running the following SQL on my database:
USE [SchoolContext-2d213d3a-3291-4719-8720-180bc8848ee7]
delete from [dbo].Student
delete from [dbo].Course
delete from [dbo].Enrollment
DBCC CHECKIDENT ('[dbo].Student', reseed,0);
Hope this helps someone else :)