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 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

I have seen this macro defined before but never really knew its purpose. Can
This really, really urks me, so I hope that someone can give me a
I am stuck! this seems really daft but I can not see where I
As far as I can tell, this is isn't possible, so I'm really just
Can I really and truly trust .NET to initialize fields (like ints, structs and
I've done some research and I can't really find a preferred way to do
Distributing ASP.NET user controls across different projects can be really useful when you want
Java is supposed to be write once, run anywhere and it really can be,
can anybody recommend some really good resources for how to get Apache authenticating users
Can someone illustrate what really is the difference between the two?

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.