I have the following classes that I am looking to add to a database:
class Parent
{
int Id;
string Name;
List<Child> Children;
}
class Child
{
int Id;
string Name;
List<Parent> Parents;
}
And am using the following to save the objects:
foreach (var parent in parents)
{
var parentModel = await this.context.Parents.FirstOrDefaultAsync(p => p.Id == parent.Id);
if (parentModel == null)
{
await this.context.Parents.AddAsync(parent);
}
else
{
this.context.Entry(parentModel).CurrentValues.SetValues(parent);
}
}
await this.context.SaveChangesAsync();
The source data parents is being created from a Web API, and can result in the same different Child objects with the same Id and Name being associated with different Parents.
e.g.
var child1 = new Child { Id = 11, Name = "Same Child ID, Different Object" };
var child2 = new Child { Id = 11, Name = "Same Child ID, Different Object" };
var parent1 = new Parent { Id = 1, Name = "1st Parent", Children = new[] { child1 } };
var parent2 = new Parent { Id = 2, Name = "2nd Parent", Children = new[] { child2 } };
var parents = new[] { parent1, parent2 };
When Entity Framework goes to add the 2nd parent the following error is received: The instance of entity type 'Child' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. This would be due to two different child objects being added with the same Id.
Is there anyway of telling Entity Framework that is OK to use one or the other child objects when saving as they are essentially the same data and should ultimately end up being the same thing? Or maybe I need to add in further code to deal with the children separately?
Or fix the underlying data so that there is only one child object and both parents reference this? This does work for me so maybe the ultimate issue is around the data relationships and I need to fully specify the relationships?