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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:43:19+00:00 2026-06-01T10:43:19+00:00

I had a bit of a struggle trying to understand why my code was

  • 0

I had a bit of a struggle trying to understand why my code was crashing (which I got to work).

When you look at both the original method and the working one, the placement of one line is different

ctx.Inventories.Attach(this);

I’m puzzled when the original method doesn’t work but the second one does.
Can anyone provide some insight?

Here’s the exception that I get.

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

This is my original method

  public void RemoveDependency(int depId)
    {
        bool returnValue = false;

        if (this.Id != 0 && depId > 0)
        {
            using (ApsEntities ctx = new ApsEntities())
            {
                var query2 = from d in ctx.Dependencies
                             where d.Id == depId
                             select d;

                Dependency found = query2.FirstOrDefault();

                if (found != null)
                {
                **ctx.Inventories.Attach(this);**
                    ctx.ObjectStateManager.ChangeObjectState(this, EntityState.Modified);
                    this.Dependencies.Remove(found);

                   ctx.SaveChanges();
                }
            }
        }

        return returnValue;
    }

Here is my working method

public void RemoveDependency(int depId)
{
    bool returnValue = false;

    if (this.Id != 0 && depId > 0)
    {
        using (ApsEntities ctx = new ApsEntities())
        {
            **ctx.Inventories.Attach(this);**

            var query2 = from d in ctx.Dependencies
                         where d.Id == depId
                         select d;

            Dependency found = query2.FirstOrDefault();

            if (found != null)
            {
                ctx.ObjectStateManager.ChangeObjectState(this, EntityState.Modified);
                this.Dependencies.Remove(found);

               ctx.SaveChanges();
            }
        }
    }

    return returnValue;
}
  • 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-01T10:43:21+00:00Added an answer on June 1, 2026 at 10:43 am

    The behavior has to do with the ObjectStateManager tracking the relationships.

    When the current Inventory item is not known to the ObjectContext (as in your first example) then the relationships between Inventory and Dependency is not recognized by the context. Since Inventory is unknown to the ObjectContext your query for a Inventory will load it and the item already exists when you attach it to your ObjectContext.

    In the second example you first attach Inventory and then execute the query. The ObjectContext will then explicitly attach the Dependency object to the Inventory item.

    The documentation states:

    ObjectStateManager tracks query results, and provides logic to merge
    multiple overlapping query results. It also performs in-memory change
    tracking when a user inserts, deletes, or modifies objects, and
    provides the change set for updates. This change set is used by the
    change processor to persist modifications.

    This behavior can’t happen when the related objects are not known to the ObjectContext.

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

Sidebar

Related Questions

We had a bit of an incident today which has got me thinking. We
I've recently had a bit of code work perfectly on the simulator, and screw
It seems like I had to inline quite a bit of code here. I'm
I have had a bit of a look around and am having some difficulty
I have (well had) a bit of code that would get me my lovely
I've had a bit of a problem trying to bind a generic list to
had a bit of confusion going around in work earlier. Thought id run it
I had a bit of conditional code that looked like this: if self.above and
I've had quite a bit of trouble trying to write a function that checks
Possible Duplicate: Why don’t self-closing script tags work? I had this bit of javascript

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.