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

  • Home
  • SEARCH
  • 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 7707925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:28:54+00:00 2026-06-01T00:28:54+00:00

I have a situation where I have an object that is loaded back from

  • 0

I have a situation where I have an object that is loaded back from a form to MVC controller via an action. We do not use FormCollection, but the one that use directly the class.

    [HttpPost]
    public ActionResult AjaxUpdate(Customer customer) { ...

The Customer object contain an object called customer which seem to be updated but when using SaveDatabase() on the context simply doesn’t work.

To make it works I had to use in the action:

myDbContext.Customers.Attach(customer) 
//...Code here that set to the customer.SubObject a real object from the database so I am sure that the SubObject contain an id which is valid and the datacontext is aware of it...
myDbContext.Entry(customer).State = EntityState.Modified; 

Still, I had an exception concerning the “Store update, insert, or delete statement affected an unexpected number of rows (0)” that I were able to remove by using:

Database.ObjectContext().Refresh(RefreshMode.ClientWins,customer);

So, to warp up my question, why do I have to Attach + change the state + call Refresh. Isn’t there a better way to update an object that contain object that are referenced in an other table. I am using Code first Entity Framework (Poco object). Also, I do not like to use Refresh since it’s hidden from my Databasecontext.

  • 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-01T00:28:55+00:00Added an answer on June 1, 2026 at 12:28 am

    I’ve made a console test project with EF 4.3.1. The code is my guess what you mean with the commented line and your comments below the question (but my guess is probably wrong because the program doesn’t reproduce your error):

    You can copy the code into program.cs and add a reference to EF 4.3.1:

    using System.Data;
    using System.Data.Entity;
    
    namespace EFUpdateTest
    {
        public class Customer
        {
            public int Id { get; set; }
            public string Name { get; set; }
    
            public int SubObjectId { get; set; }
            public SubObject SubObject { get; set; }
        }
    
        public class SubObject
        {
            public int Id { get; set; }
            public string Something { get; set; }
        }
    
        public class CustomerContext : DbContext
        {
            public DbSet<Customer> Customers { get; set; }
            public DbSet<SubObject> SubObjects { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                int customerId = 0;
                int subObject1Id = 0;
                int subObject2Id = 0;
                using (var ctx = new CustomerContext())
                {
                    // Create customer with subobject
                    var customer = new Customer { Name = "John" };
                    var subObject = new SubObject { Something = "SubObject 1" };
                    customer.SubObject = subObject;
                    ctx.Customers.Add(customer);
    
                    // Create a second subobject, not related to any customer
                    var subObject2 = new SubObject { Something = "SubObject 2" };
                    ctx.SubObjects.Add(subObject2);
    
                    ctx.SaveChanges();
    
                    customerId = customer.Id;
                    subObject1Id = subObject.Id;
                    subObject2Id = subObject2.Id;
                }
    
                // New context, simulate detached scenario -> MVC action
                using (var ctx = new CustomerContext())
                {
                    // Changed customer name
                    var customer = new Customer { Id = customerId, Name = "Jim" };
                    ctx.Customers.Attach(customer);
    
                    // Changed reference to another subobject
                    var subObject2 = ctx.SubObjects.Find(subObject2Id);
                    customer.SubObject = subObject2;
    
                    ctx.Entry(customer).State = EntityState.Modified;
    
                    ctx.SaveChanges();
                    // No exception here.
                }
            }
        }
    }
    

    This works without exception. The question is: What is different in your code which could cause the error?

    Edit

    To your comment that you don’t have a foreign key property SubObjectId in the customer class: If I remove the property in the example program above I can reproduce the error.

    The solution is to load the original subobject from the database before you change the relationship:

    // Changed customer name
    var customer = new Customer { Id = customerId, Name = "Jim" };
    ctx.Customers.Attach(customer);
    
    // Load original SubObject from database
    ctx.Entry(customer).Reference(c => c.SubObject).Load();
    
    // Changed reference to another subobject
    var subObject2 = ctx.SubObjects.Find(subObject2Id);
    customer.SubObject = subObject2;
    
    ctx.Entry(customer).State = EntityState.Modified;
    
    ctx.SaveChanges();
    // No exception here.
    

    Without a foreign key property you have an Independent Association which requires that the object including all references must represent the state in the database before you change it. If you don’t set the reference of SubObject in customer EF assumes that the original state in the database is that customer does not refer to any subobject. The generated SQL for the UPDATE statement contains a WHERE clause like this:

    WHERE [Customers].[Id] = 1 AND [Customers].[SubObject_Id] IS NULL
    

    If the customer has a subobject in the DB [SubObject_Id] is not NULL, the condition is not fulfilled and the UPDATE does not happen (or happens for the “unexpected number of rows 0”).

    The problem does not occur if you have a foreign key property (Foreign Key Association): The WHERE clause in this case is only:

    WHERE [Customers].[Id] = 1
    

    So, it doesn’t matter what’s the original value of SubObject and of SubObjectId. You can leave the values null and the UPDATE works nonetheless.

    Hence, the alternative solution to loading the original subobject is to introduce a foreign key property in Customer:

    public int SubObjectId { get; set; }
    

    Or, in case the relationship is not required:

    public int? SubObjectId { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the situation: I have a massive object that needs to be loaded into
The situation is this: You have a Hibernate context with an object graph that
Situation:- In my code I have to use the LWUIT Component object for the
I have an MVC/Nhibernate app that is giving me the below. [WrongClassException: Object with
I have a specific situation where I've got an object that I want to
I have a situation where I'm starting a number of objects that, when they
I have a situation where an object A has a reference to object B.
So, I have this situation where I need to see if an object is
I ran into this situation today. I have an object which I'm testing for
I have quite a simple situation. I've got 3 objects: an object called TrainingConsultant

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.