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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:11:22+00:00 2026-05-30T10:11:22+00:00

This has been bugging me for some time now. My problem is the following.

  • 0

This has been bugging me for some time now. My problem is the following. I have a instance of type X that is called x. X is an entity class that is generated by Entity Framework.

Inside my method I receive x from a web service, so it’s not attached to my object context, but I know that an element with the same primary key exist in the database already, so I want to update it. I added the following body to the method, and it worked fine:

objectContext.Xs.Attach(x);
objectContext.ObjectStateManager.ChangeObjectState(x, EntityState.Modified);
objectContext.SaveChanges();

So far all good. However my objectContext is not created by this method, and suddenly this code stopped working and threw a InvalidOperationException with the following text:

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

This happened because sometime before this code was run, the following code was executed:

objectContext.Xs.ToList();

This read all the current X objects from the database, and thereby added them to the ObjectStateManager.

I then changed by code to this:

objectContext.Xs.ApplyCurrentValues(x);
objectContext.SaveChanges();

This worked fine, but if I remove the objectContext.Xs.ToList(); line again, I get another InvalidOperationException with the following text:

An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied.

So now I got the opposite problem. This method does not work because it’s not tracked by the ObjectStateManager.

In addition I haven’t found any reliable method of determining if my x is already tracked or not, so currently I have added the code like this:

try
{
    objectContext.Xs.Attach(x);
    objectContext.ObjectStateManager.ChangeObjectState(x, EntityState.Modified);
    objectContext.SaveChanges();
}
catch(InvalidOperationException)
{
    objectContext.Xs.ApplyCurrentValues(x);
}
objectContext.SaveChanges();

This means that I am using exception handling as normal program flow (it’s hardly exceptional that the element is already tracked by the ObjectStateManager), and I also have the problem that I might catch another unrelated InvalidOperationException that I should have handled in another way.

So my question is. Does anyone know a good solution for this, or at least a proper way to check if x is tracked by the ObjectStateManager so I can choose one of the method based on that instead of a try/catch?

  • 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-30T10:11:24+00:00Added an answer on May 30, 2026 at 10:11 am

    You need to check if object with the same EntityKey is already attached. Try something like this:

    string containerName = context.DefaultContainerName;
    string setName = context.CreateObjectSet<X>().EntitySet.Name;
    EntityKey key = new EntityKey(String.Format("{0}.{1}", containerName, setName), 
                                  "KeyProperty", x.KeyProperty);
    if (context.ObjectStataManager.GetObjectStateEntry(key) != null)
    {
        // Apply current values
    }
    else
    {
        // Attach and set state
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a question regarding something which has been bugging me for some time now.I'm
This is a question that's been bugging me for some time now: In photoshop/GIMP,
This has been bugging me for some time now, I will greatly appreciate the
This question has been bugging me for some time. I always picture launching my
This Jquery problem has been bugging me for a while now. I developed a
This problem has been bugging me for a while. I have to load a
This has been bugging me for a while. I have this ircbot that I
This problem has been bugging me since forever. I have an array and in
This is has been bugging me for quite some time. >> nil.id (irb):2: warning:
This has been bugging me for a long time now and I can't seem

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.