Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:35:35+00:00 2026-06-04T21:35:35+00:00

I’m having a strange issue. I have a database with existing items, I added

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:35:36+00:00Added an answer on June 4, 2026 at 9:35 pm

    I think you’re confusing ASP.NET MVC validation with Entity Framework’s validation.

    • ModelState.IsValid checks the validity of the models that were bound to your MVC Action’s parameters.
    • Entity Framework does its own validation of the entities that it tries to save.

    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.IsValid is 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:

    var models = db.Model.Where(f => bar.Contains(f.Id)).ToList();
    TryUpdateModel(models);
    if(ModelState.IsValid)
    {
        foreach(var model in models)
        {
            model.updated = true;
            db.Entry(model).State = EntityState.Modified;
        }
        db.SaveChanges();
    }
    

    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.Errors property 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.

    db.Configuration.ValidateOnSaveEnabled = false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.