entity relation object update

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

Answers


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.


Need Your Help

Local Roles for ATBTreeFolder vs ATFolder

plone archetypes

I have a Plone site that is upgraded from Plone 3.3.5 to 4.0.7. From ZMI, I find there are 2 kinds of folders: ATBTreeFolder and ATFolder. With ATBTreeFolder, I can assign Local Roles in Security t...

How do I get a collection (or a stream) of distinct IP addresses using Tor?

proxy ip ip-address web-crawler tor

I'm writing a web crawler for academic research. This crawler makes millions of requests that I want to distribute over ten or so IP addresses.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.