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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:47:45+00:00 2026-05-14T22:47:45+00:00

Can EF really be this bad? Maybe… Let’s say I have a fully loaded,

  • 0

Can EF really be this bad? Maybe…

Let’s say I have a fully loaded, disconnected object graph that looks like this:

myReport = 
{Report}
  {ReportEdit {User: "JohnDoe"}}
  {ReportEdit {User: "JohnDoe"}}

Basically a report with 2 edits that were done by the same user.

And then I do this:

EntityContext.Attach(myReport);

InvalidOperationException: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

Why? Because the EF is trying to attach the {User: "JohnDoe"} entity TWICE.

This will work:

myReport =
{Report}
  {ReportEdit {User: "JohnDoe"}}

EntityContext.Attach(myReport);

No problems here because the {User: "JohnDoe"} entity only appears in the object graph once.

What’s more, since you can’t control how the EF attaches an entity, there is no way to stop it from attaching the entire object graph. So really if you want to reattach a complex entity that contains more than one reference to the same entity… well, good luck.

At least that’s how it looks to me. Any comments?

UPDATE: Added sample code:


// Load the report 
Report theReport;
using (var context1 = new TestEntities())
{
    context1.Reports.MergeOption = MergeOption.NoTracking;
    theReport = (from r in context1.Reports.Include("ReportEdits.User")
                 where r.Id == reportId
                 select r).First();
}

// theReport looks like this:
// {Report[Id=1]}
//   {ReportEdit[Id=1] {User[Id=1,Name="John Doe"]}
//   {ReportEdit[Id=2] {User[Id=1,Name="John Doe"]}

// Try to re-attach the report object graph
using (var context2 = new TestEntities())
{
    context2.Attach(theReport); // InvalidOperationException
}
  • 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-05-14T22:47:45+00:00Added an answer on May 14, 2026 at 10:47 pm

    The problem is that you modified the default MergeOption:

    context1.Reports.MergeOption = MergeOption.NoTracking;
    

    Entities retrieved with NoTracking are intended for read-only use because there is no fixup; this is in the documentation for MergeOption. Because you set NoTracking, you now have two entirely separate copies of {User: "JohnDoe"}; without fixup the “duplicate” references don’t get boiled down to a single instance.

    Now when you try to save “both” copies of {User: "JohnDoe"}, the first succeeds in being added to the context, but the second can’t be added because of the key violation.

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

Sidebar

Related Questions

So, maybe this is a bad design; I don't know. But say I have
I have read that you can do it, but would this really improve performance
I'm really confused here. Let's say my user story looks like this. A user
I really can't work out how to best do this, I can do fairly
I really hate this error, because it can be so hard to pin point.
I can't really think of the best way to phrase this question, so I'll
This really shouldn't be hard, I just can't figure out how to do it.
This really seems like a bug to me, but perhaps some databinding gurus can
This really, really urks me, so I hope that someone can give me a
Sorry for the vague title, as I really can't explain this problem succinctly. Basically

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.