2

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?

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151

1 Answers1

4

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
}
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • This is really close to what I need. My only problem is that (as i forgot to mention in my original question) X is the type inside a generic class. So all I know of X is that it is of type EntityObject. Are there any way I can find out which parameters make up the EntityKey just from that information? I guess that the object context already know since it's complaining that the keys are not unique. – Øyvind Bråthen Feb 23 '12 at 11:53
  • Check [this answer](http://stackoverflow.com/questions/5794902/generic-getbyid-for-complex-pk/5795480#5795480) - I described how to get information about keys from the entity. If all your entities have only single key property you can use [this simplified version](http://stackoverflow.com/questions/5273416/entity-framework-simple-generic-getbyid-but-has-differents-pk-name/5278684#5278684) – Ladislav Mrnka Feb 23 '12 at 11:57
  • Also if you have your entities derived from `EntityObject` it should expose `EntityKey` directly so if you have it correctly filled you don't need to use all this helper stuff normally used only for POCOs. – Ladislav Mrnka Feb 23 '12 at 12:05
  • I got it to work at last. The other problem I found was that GetObjectStateEntry does not return null, but throws an exception. So I ended up with the following solution: 1. I used TryGetObjectStateEntry instead, and 2. I used `objectContext.CreateEntityKey(objectset.EntitySet.Name, x)` to get the EntityKey, and now it works perfectly. Thanks for putting me on the right track and finding a solution to this problem. – Øyvind Bråthen Feb 23 '12 at 12:17