I am using GraphDiff to update complicate object graphs. My main entity has multiple children that may be created, updated or deleted (OwnedCollections in graphDiff). So in GraphDiff when an entity gets updated, a different copy of the entity is being created and you have to get this copy in order to get Ids of newly created entities etc. I encountered though the following problem: When I add an entity to a child collection, the returned "mother" entity has the same child twice. I have downloaded the code and the following code in CollectionGraphNode seems to cause the issue:
private object AddElement<T>(IChangeTracker changeTracker, IEntityManager entityManager, T existing, object updateItem, object dbCollection)
{
// My comment: input parameter dbCollection contains the existing child Entities
if (!_isOwned)
{
updateItem = changeTracker.AttachAndReloadAssociatedEntity(updateItem);
}
else if (changeTracker.GetItemState(updateItem) == EntityState.Detached)
{
var instance = entityManager.CreateEmptyEntityWithKey(updateItem);
// My comment: dbCollection here has existing children (e.g.1)
changeTracker.AddItem(instance);
changeTracker.UpdateItem(updateItem, instance);
//My comment: dbCollection here has +1 (2)
foreach (var childMember in Members)
{
childMember.Update(changeTracker, entityManager, instance, updateItem);
}
updateItem = instance;
}
dbCollection.GetType().GetMethod("Add").Invoke(dbCollection, new[] {updateItem});
//My comment: dbCollection here has again +1 and thus twice my new entity
if (_isOwned)
{
changeTracker.AttachCyclicNavigationProperty(existing, updateItem, GetMappedNaviationProperties());
}
return updateItem;
}
I know my question is very specific but GraphDiff is my last hope that I 'll be able to update my object in a neat way. I am not that good in EF and I am trying to understand how the above two lines of code add my entity twice in the list. My child entity has reference to the parent, could that cause the extra addition? Has anyone had a similar issue and how did you resolved it?