I’m having a strange issue. I have a database with existing items, I added a new validation rule so some of the items in the database don’t comply with this new rule.
I have a loop that finds a record, changes an element then saves it back to the database, like this:
foreach(int foo in bar)
{
Model model = db.Model.Find(foo);
model.updated = true;
if(ModelState.IsValid)
{
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
}
}
I figured that for records that don’t comply to the new validation rule would just not be updated because the ModelState.IsValid wouldn’t pass. This is not the case though, it trows a validation failed exception. So I put it in a try catch and figured while I’m at it I’ll log the error so I know what records arn’t valid. So it now looks like this:
foreach(int foo in bar)
{
Model model = db.Model.Find(foo);
model.updated = true;
try
{
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
}
catch(Exception x)
{
// log error
if(ModelState.IsValid)
{
db.ErrorLogs.Add(errorLog);
db.SaveChanges();
}
}
}
This also trows a validation failed exception, i guess because the exception in the try is not cleared. So fine, I decide to just try, and not catch the error. So it looks like this:
foreach(int foo in bar)
{
Model model = db.Model.Find(foo);
model.updated = true;
try
{
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
}
catch()
{
}
}
Now what ends up happening is that say I have 100 records that are cycled through the foreach loop, if #27 fails validation every record that comes after fails and therefore is not updated!
This is very aggravating, how can I fix this? Is there a way to clear the validation error? Why is the error persisting to all the other loops? Is it because db was declared outside the loop? And why is it passing the ModelState.IsValid in the first place?
Thanks
I think you’re confusing ASP.NET MVC validation with Entity Framework’s validation.
ModelState.IsValidchecks the validity of the models that were bound to your MVC Action’s parameters.It appears that the aspects of your model which aren’t valid are not those which were sent to your action method, but rather those which came out of the database. So when the model binding occurred, you had a valid object, and
ModelState.IsValidis true. But when you try to save the objects, Entity Framework still detects that they are invalid and throws an exception.It seems odd that you would create validation on your entities that causes current data in the database to be incorrect. Consider running a SQL script to correct the data in the database.
If you can’t do that, you could try binding the entities after having loaded them out of the database:
Of course, the above code only works if all of the model are valid. If you want to selectively update only the correct ones, you could check the
ModelState.Errorsproperty for errors pertaining to each model entry.If you really want to suppress entity validation in this particular case, bearing in mind that this could be dangerous, you can simply disable validation on your context before saving.