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

Can someone please tell me where I'm going wrong with this piece of code?
can somebody please tell me where i m wrong why this RMI chat application
Can anyone tell me what's wrong with this robots.txt? http://bizup.cloudapp.net/robots.txt The following is the
Please can someone tell me what im doing wrong here with respect to calling
Can you please tell me how much is (-2) % 5 ? According to
Can anyone please tell me if Certifying Authorities (CAs) are allowed to make modifications
Could someone please tell me which objects types can be tested using Regular Expressions
can anyone please suggest a good code example of vb.net/c# code to put the
Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how
I can't seem to get this text to align vertically. Can someone please tell

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.