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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:24:16+00:00 2026-05-22T12:24:16+00:00

Please can you tell me where I am wrong. I had a following code:

  • 0

Please can you tell me where I am wrong. I had a following code:

public void UpdateClient(Client oClient)
    {                                        
        foreach(Mitarbeiter item in oClient.Mitarbeiters)
        {
            if (item.MiID==0)
            {                    
                context.Mitarbeiters.AddObject(item);                    
            }
            else {
                var key = context.CreateEntityKey("Mitarbeiters",item);
                object original;
                if (context.TryGetObjectByKey(key,out original))
                {
                    context.ApplyCurrentValues(key.EntitySetName,item);
                }
            }
        }
        context.Clients.First(c => c.ClID == oClient.ClID);            
        context.Clients.ApplyCurrentValues(oClient);


       context.SaveChanges();

}
I received

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

when I add a new object to Mitarbeiters

  • 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-22T12:24:18+00:00Added an answer on May 22, 2026 at 12:24 pm

    Assuming that your class Mitarbeiter has a navigation property to Client and it is not null when you run through the loop (so item.Client != null) then by adding item to the ObjectSet you also add the referenced Client into the context in state Added. (Adding an entity does not only add the entity itself but also all referenced entities which are not yet in the context.) Later (context.Clients.First(c => c.ClID == oClient.ClID);) you load the client a second time which is already in the context in Added state which is the reason for the exception.

    Try to load the client into the context before you add item:

    public void UpdateClient(Client oClient)
    {                                        
        context.Clients.First(c => c.ClID == oClient.ClID);
    
        foreach(Mitarbeiter item in oClient.Mitarbeiters)
        {
            if (item.MiID==0)
            {                    
                context.Mitarbeiters.AddObject(item);                    
            }
            else {
                var key = context.CreateEntityKey("Mitarbeiters",item);
                object original;
                if (context.TryGetObjectByKey(key,out original))
                {
                    context.ApplyCurrentValues(key.EntitySetName,item);
                }
            }
        }
        context.Clients.ApplyCurrentValues(oClient);
    
        context.SaveChanges();
    }
    

    Just a guess, I am not sure if this will solve your problem.

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

Sidebar

Related Questions

All, Can u please tell me whats wrong with the following code.I am trying
Can someone please tell me whats wrong with this code. I'm not getting complete
Can somebody please tell me what is wrong with the following pom.xml? i'm getting
Please can someone really tell me what's wrong with this code that I'm having
Can someone tell me please why the following example work in Firefox but not
Please can someone tell me what im doing wrong here with respect to calling
Hi can someone please tell me what wrong with this line (getting an error...)
Can someone please tell me whats wrong with this trigger statement? DELIMITER // CREATE
I need to update jquery1.3.2 to jquery1.4 (in Asp.net MVC). Please can anyone tell
Please can some one briefly tell me with example what does the means of

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.