Reputation: 553
I'm using Entity Framework 6 and creating seed data for a test database. All is working except for one problem.
Some of my classes check for a duplicate ID before adding an object to a list. This is a typical pattern:
private Assignment FindAssignment(VideoCourse course) {
Assignment assignment = assignments.Find(x => x.Course.Id == course.Id);
if (assignment == null) {
assignment = new Assignment(this, course);
assignments.Add(assignment);
}
return assignment;
}
This fails (I can't add more than one course) in my Seed override because course.Id
is 0 for all courses until they've been saved to the database.
I've tried calling SaveChanges()
at multiple places in the code but that really messes with the context and results in other objects not being saved.
So the question - how do I get objects saved to the database so their Id's can be set?
Upvotes: 0
Views: 251
Reputation: 553
It turned out that the easiest way to solve the problem was to assign objects their Id in code, rather than waiting for the database to do it.
Upvotes: 1