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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:08:36+00:00 2026-06-13T23:08:36+00:00

I’m having a problem updating a disconnected POCO model in an ASP.NET application. Lets

  • 0

I’m having a problem updating a disconnected POCO model in an ASP.NET application.

Lets say we have the following model:

  1. Users
  2. Districts
  3. Orders

A user can be responsible for 0 or more districts, an order belongs to a district and a user can be the owner of an order.

When the user logs in the user and the related districts are loaded. Later the user loads an order, and sets himself as the owner of the order. The user(and related districts) and order(and related district) are loaded in two different calls with two different dbcontexts. When I save the order after the user has assigned himself to it. I get an exception that saying that acceptchanges cannot continue because the object’s key values conflict with another object.

Which is not strange, since the same district can appear both in the list of districts the user is responsible and on the order.

I’ve searched high and low for a solution to this problem, but the answers I have found seems to be either:

  1. Don’t load the related entities of one of the objects in my case that would be the districts of the user.
  2. Don’t assign the user to the order by using the objects, just set the foreign key id on the order object.
  3. Use nHibernate since it apparently handles it.

I tried 1 and that works, but I feel this is wrong because I then either have to load the user without it’s districts before relating it to the order, or do a shallow clone. This is fine for this simple case here, but the problem is that in my case district might appear several more times in the graph. Also it seems pointless since I have the objects so why not let me connected them and update the graph. The reason I need the entire graph for the order, is that I need to display all the information to the user. So since I got all the objects why should I need to either reload or shallow clone it to get this to work?

I tried using STE but I ran in to the same problem, since I cannot attach an object to a graph loaded by another context. So I am back at square 1.

I would assume that this is a common problem in anything but tutorial code. Yet, I cannot seem to find any good solution to this. Which makes me think that either I do not under any circumstance understand using POCOs/EF or I suck at using google to find an answer to this problem.

I’ve bought both of the “Programming Entity Framework” books from O’Reilly by Julia Lerman but cannot seem to find anything to solve my problem in those books either.

Is there anyone out there who can shed some light on how to handle graphs where some objects might be repeated and not necessarily loaded from the same context.

  • 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-13T23:08:37+00:00Added an answer on June 13, 2026 at 11:08 pm

    The reason why EF does not allow to have two entities with the same key being attached to a context is that EF cannot know which one is “valid”. For example: You could have two District objects in your object graph, both with a key Id = 1, but the two have different Name property values. Which one represents the data that have to be saved to the database?

    Now, you could say that it doesn’t matter if both objects haven’t changed, you just want to attach them to a context in state Unchanged, maybe to establish a relationship to another entity. It is true in this special case that duplicates might not be a problem. But I think, it is simply too complex to deal with all situations and different states the objects could have to decide if duplicate objects are causing ambiguities or not.

    Anyway, EF implements a strict identity mapping between object reference identity and key property values and just doesn’t allow to have more than one entity with a given key attached to a context.

    I don’t think there is a general solution for this kind of problem. I can only add a few more ideas in addition to the solutions in your question:

    • Attach the User to the context you are loading the order in:

      context.Users.Attach(user); // attaches user AND user.Districts
      var order = context.Orders.Include("Districts")
          .Single(o => o.Id == someOrderId);
      // because the user's Districts are attached, no District with the same key
      // will be loaded again, EF will use the already attached Districts to
      // populate the order.Districts collection, thus avoiding duplicate Districts
      order.Owner = user;
      context.SaveChanges();
      // it should work without exception
      
    • Attach only the entities to the context you need in order to perform a special update:

      using (var context = new MyContext())
      {
          var order = new Order { Id = order.Id };
          context.Orders.Attach(order);
          var user = new User { Id = user.Id };
          context.Users.Attach(user);
      
          order.Owner = user;
      
          context.SaveChanges();
      }
      

      This would be enough to update the Owner relationship. You would not need the whole object graph for this procedure, you only need the correct primary key values of the entities the relationship has to be created for. It doesn’t work that easy of course if you have more changes to save or don’t know what exactly could have been changed.

    • Don’t attach the object graph to the context at all. Instead load new entities from the database that represent the object graph currently stored in the database. Then update the loaded graph with your detached object graph and save the changes applied to the loaded (=attached) graph. An example of this procedure is shown here. It is safe and a very general pattern (but not generic) but it can be very complex for complex object graphs.

    • Traverse the object graph and replace the duplicate objects by a unique one, for example just the first one with type and key you have found. You could build a dictionary of unique objects that you lookup to replace the duplicates. An example is here.

    • 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 a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
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.