I'm getting a DbEntityValidationException from a controller method that's trying to set the boolean IsVisible
on an entity it retrieves from the DB. It's responding to an AJAX post from a change in a tick box on the page. This code used to work.
var targetClass = db.Classes.FirstOrDefault(x => x.ID == cid);
targetClass.IsVisible = true;
db.SaveChanges();
This results in DbEntityValidationException
with the following errors:
The SchoolYear field is required.
The TuitionPlan field is required.
When I step through this code both targetClass.SchoolYear
and targetClass.TuitionPlan
are valid.
Question is, how do I figure out why EF thinks these fields are missing?
EDIT: This may have to do with (too) lazy loading... If I use both of the "missing" fields, the error goes away. Probably nothing more worrisome than not knowing why a serious problem just went away.
var targetClass = db.Classes.FirstOrDefault(x => x.ID == cid);
targetClass.IsVisible = value;
int x = targetClass.TuitionPlan.ID;
x = targetClass.SchoolYear.ID;
db.SaveChanges();
I really need someone to explain what's going on here, and how I should prevent this in the future.
Thanks for insight, Eric