I tried loading the navigation properties first by using Include
, and I ensured that the children were loaded.
For new items, I can add a child navigation set with no issues. For updated items, I tried to pass in a completely new collection to overwrite the list, but I read that I needed to work with the loaded collection. Even after clearing the collection and adding to it, I do not see any changes reflected.
For all entities in my project, I am calling a generic method which has these lines and some error handling logic, but no errors are thrown:
Save() method:
context.Set<T>().AddOrUpdate(entities);
context.SaveChanges();
Use:
var entities = repository.LoadWithIncludes(x =>.Subset1);
var entity = entities.FirstOrDefault(x => x.ID == id) ?? new Entity{ID = id};
entity.Subset1.Clear();
var IDs = GetIDs(id) ?? Array.Empty<int?>();
foreach (var id in IDs)
{
entity.Subset1.Add(new Subset
{
ParentId = id,
Value = part1;
});
}
// then I pass the new and updated items into an array and call the Save() method above
The LoadWithIncludes method is taken from this answer: https://stackoverflow.com/a/18805096/7096114