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 8011913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:09:45+00:00 2026-06-04T19:09:45+00:00

I have the following Model public class CourseModel { [Key] public int courseID {

  • 0

I have the following Model

public class CourseModel
{
    [Key]
    public int courseID { get; set; }
    ...
    public virtual ICollection<CourseMeetModel> meets { get; set; }
}

When I try to edit one of the entries and if the input is valid it works fine.
However if the if its not valid it alerts the user of the mistakes. Once the user fixes the mistakes and tries to save i get the following exception.

Store update, insert, or delete statement affected an unexpected
number of rows (0). Entities may have been modified or deleted since
entities were loaded. Refresh ObjectStateManager entries.

I have noticed this happens if the input fails the validation steps in my controller.

My Controller

public ActionResult EditCourseConfirmed(CourseModel course)
{
        CoursesDBContext db = new CoursesDBContext();
        bool valid = validateCouse(course); //If this fails and the course model is returned back to the view I get that error
        if (valid)
        {
                try
                {
                    db.Entry(course).State = EntityState.Modified;
                    db.SaveChanges();
                    Session[d.s_Clear] = false;
                    return RedirectToAction("Index");
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(string.Empty, "Unable to save the course, please try again." + e.Message);
                    return View(course);
                }

        }

        return View(course);
}
  • 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-04T19:09:49+00:00Added an answer on June 4, 2026 at 7:09 pm

    Try this

    try {
       context.SaveChanges();
    } 
    catch (OptimisticConcurrencyException) 
    {
        context.Refresh(RefreshMode.ClientWins, db.table);
        context.SaveChanges();
    }
    

    Props: https://stackoverflow.com/a/6848729/1166147

    Adding on to explain, per your comments. Thanks for your EF4 CTP5 tip. Glad this fixed it, please mark as accepted. When you get this error, something happened that changed the data (another user, etc) between load and update, there is a trigger causing problems, or if using a stored procedure it hit 0 recs. It is hard to know without more info. Is this the first update hit? Was there another update run first that succeeded before the user hit the error, modified, and then tried to go on without refreshing? Do you have any triggers? What is your degree of concurrency – did another user edit and save between this users’ query and update? Read the link to the post I gave – someone mentioned a ReadOnlyAttribute in an entity key’s metadata being replaced, which causes its value to become zero as a potential cause – I am not sure about this, but it makes sense –

    (Modified from MSDN) By default, the Entity Framework implements an optimistic concurrency model. This means that locks are not held on data in the data source between when the data is queried and the data is updated, creating the potential for this error if another user modified the data. When this attribute is used, the Entity Framework checks for changes in the database before saving changes.

    Any conflicting changes will cause an OptimisticConcurrencyException.

    An OptimisticConcurrencyException can also occur when you define an Entity Data Model that uses stored procedures to make updates to the data source. In this case, the exception is raised when the stored procedure that is used to perform updates reports that zero rows were updated. SET NOCOUNT ON would fix thia.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model: public class A { public int? Id {get;set;} public
I have the following model: public class Product { public int Id { get;
I have the following model entity: public class ScheduledTask { public virtual int ScheduledTaskId
I have the following model : public class Foo { [Key] public int FooID
I have the following model: public class Blog { public int BlogID { get;
I have the following model: public class Hotfix { public int? released_version { get;
I have the following Model: public class Person { public string Name {get;set;} public
I have the following model public class Account { public int Id { get;
I have the following entity model: public class Project { [Key] public int ProjectID
I have the following view model: public class BudgetTypeSiteRowListViewModel { public virtual int BudgetTypeSiteID

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.