GraphDiff: GraphDiff duplicate returned entities
Hello, As I’ve said on stackoverflow, I have a problem adding new entities to my ‘parent’ entity’s list of children using GraphDiff. I’ll try to reproduce my situation and show the problem.
I have an entity called FilterTemplate
that has a list of FilterConditions
. So in reality the class FilterTemplate
has a list of FilterConditions
and the class FilterCondition
has an attribute
public virtual FilterTemplate FilterTemplate { get; set; }
and EntityFramework does the rest for me. The list of FilterConditions
can change as the user may add, update or delete conditions but I don’t know exactly what and that’s why I need GraphDiff.
Classes:
public class FilterTemplate
{
public FilterTemplate()
{
this.FilterConditions = new List<FilterCondition>();
}
public int Id { get; set; }
public string TemplateName { get; set; }
public virtual ICollection<FilterCondition> FilterConditions { get; set; }
}
public class FilterCondition
{
public int Id { get; set; }
public int FilterTemplateId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public virtual FilterTemplate FilterTemplate { get; set; }
}
For example, I have exactly one filterCondition and the user adds another one, then when I want to update the list I call
dbContext.UpdateGraph(filterTemplate, map => map.OwnedCollection(ft => ft.FilterConditions));
This leads me to CollectionGraphNode.Update<T> where variable ‘existing’ has a list of one item and ‘entity’ has a list of 2 items with the 2nd having an Id=0. Variable ‘dbCollection’ gets populated with one filterCondition. When the loop gets to the new filterCondition, AddElement<T> gets called. There as I said before calling “changeTracker.UpdateItem(updateItem, instance);” dbCollection has one item and after it two (existing variable has also a child list of 2). When the code gets to
dbCollection.GetType().GetMethod("Add").Invoke(dbCollection, new[] {updateItem});
the dbCollections has now 3 items with the last two being exactly the same (similarly in variable existing). From that point onwards the list keeps having my addition as duplicate (with the same new Id) and of course causing a lot of trouble.
Commenting out the line
dbCollection.GetType().GetMethod("Add").Invoke(dbCollection, new[] {updateItem});
seems to fix the issue but I don’t find it proper to change the code that I honestly don’t get completely as drilling down to it will take way too much time. So I was wondering if you are aware that is an issue with the Add logic or if my case’s specifics are the cause of the problem. I don’t know if more code snippets will help as most of the logic lies with GraphDiff. But if so, let me know. Thanks in advance.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 25 (1 by maintainers)
This is still an issue with version 3.0.0, would be awesome if this was fixed! 😃
I have the same problem.
My current (and crude) solution is to do the following before calling
UpdateGraph
:if (entity.Children != null) { entity.Children .ToList().ForEach(x => { x.ParentId = x.Id == 0 ? 0 : entity.Id; x.ChildId = x.Child.Id; }); }
Is there a better and more reliable way instead of the above?