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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:52:49+00:00 2026-06-12T21:52:49+00:00

BACKGROUND I’m having some trouble with updating an entity in EF. I keep getting

  • 0

BACKGROUND

I’m having some trouble with updating an entity in EF. I keep getting this error:

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

I am fully aware that there is obviously another entity somewhere that is attached. However, I currently cannot track it down. There is a lot of code and I’ve spent quite some time on it already. As far as I can see, I am using the AsNoTracking() extension method on all my queries.

WHAT I NEED

My question is this: Is there any way I can see what is actually in the ObjectStateManager at any given time? If I can see the items in there during debugging, I can more quickly track down where this is coming from.

If the above is not possible, I would appreciate any advice on how best to tackle this problem.. it’s like a needle in a haystack right now.

  • 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-12T21:52:52+00:00Added an answer on June 12, 2026 at 9:52 pm

    This question helped:

    what is the most reasonable way to find out if entity is attached to dbContext or not?

    and I implemented it in this way:

    var attachedEntity = context.ChangeTracker.Entries<T>().FirstOrDefault(x => x.Entity.Id == entity.Id);
    
                    // If the entity is already attached. 
                    if (attachedEntity != null)
                    {
                        // Set new values
                        attachedEntity.CurrentValues.SetValues(entity);
                    }
                    else
                    {
                        // Else attach the entity (if needed)
                        if (context.Entry(entity).State == EntityState.Detached)
                        {
                            Entities.Attach(entity);
                        }
                        // Set the entity's state to modified
                        context.Entry(entity).State = EntityState.Modified;
                    }
                    context.SaveChanges();
    

    Note: Entities is just IDbSet<T> from context.Set<T>() and the code above is from the Update() method in my generic repository.

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

Sidebar

Related Questions

Background I'm working on an application for my own needs, with some very lightweight
Background I have an installation of VisualSVN on a server. under this, I have
Background info: Me and a couple of friends are building this platform game in
Background: I have a website where people can store transactions. As part of this
Background Information I'm trying to build the GAUL library according to this Instructions .
Background: Some time ago, I built a system for recording and categorizing application crashes
Background I am trying to create a copy of a business object I have
Background So, with iOS 6 an UITextView can take an attributedString, which could be
Background I'm trying to understand matrix transformations. (Developing plugins for SketchUp using it's Ruby
Background : I am using nginx as a reverse proxy to my express server

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.