TryGetObjectStateEntry returns false but when i try to attach the entity i get 'An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.'
The entity key is of type Guid.
How is this possible?
Edit: i am attaching 2 entities with different key. the error occurs always on the second entity of this type that i attach. if i swap them the error is still on the 2nd one.
public bool IsAttached<T>(T obj) where T : class
{
ObjectStateEntry entry = null;
ObjectContext objCtx = GetObjectContext();
bool isKeyAttached = false;
EntityContainer container = objCtx.MetadataWorkspace.GetEntityContainer(objCtx.DefaultContainerName, DataSpace.CSpace);
EntitySetBase entitySet = container.BaseEntitySets.Where(item => item.ElementType.Name.Equals(typeof(T).Name)).FirstOrDefault();
System.Data.EntityKey key = objCtx.CreateEntityKey(entitySet.Name, obj);
if (objCtx.ObjectStateManager.TryGetObjectStateEntry(key, out entry))
{
isKeyAttached = entry.State != System.Data.EntityState.Detached;
}
return isKeyAttached;
}