How to Replace child collections in Entity Framework?
💻 coding

How to Replace child collections in Entity Framework?

1 min read 154 words
1 min read
ShareWhatsAppPost on X
  • 1Replacing child collections in Entity Framework requires removing existing entities before adding new ones to avoid key conflicts.
  • 2An extension method can simplify the process of replacing entity collections by handling removals and additions efficiently.
  • 3The provided example demonstrates how to use the extension method to replace ingredients in a product's ingredient collection.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"Replacing child collections in Entity Framework requires removing existing entities before adding new ones to avoid key conflicts."

How to Replace child collections in Entity Framework?

ORMs are nice. Usually. That is until you hit a mostly unhelpful exception like “An object with the same key already exists in the ObjectStateManager”. I ran into this problem by trying to do a complete replacement of an entity’s child entity collection (i.e. a Products’s IQueryable<Ingredient>). To do this, you actually need to remove all of the entities from the IQueryable that need to be removed, and then add the new entities. After some frustration, I created the following extension to help me out.

public static void ReplaceEntityCollection<T, K>(this ICollection<T> startingCollection, ICollection<T> newCollection,
 Func<T, K> selector )
{
 var newItemCompareValues = newCollection.Select(selector);
 var existingItemCompareValues = startingCollection.Select(selector);

 var alternativeItemsToRemove = startingCollection.Where(at => !newItemCompareValues.Contains(selector(at)));
 var alternativeItemsToAdd = newCollection.Where(at => !existingItemCompareValues.Contains(selector(at)));

 foreach (var alternativeItemToRemove in alternativeItemsToRemove)
 {
 startingCollection.Remove(alternativeItemToRemove);
 }

 foreach (var alternativeItemToAdd in alternativeItemsToAdd)
 {
 startingCollection.Add(alternativeItemToAdd);
 }
}

Now, I can do the following:

myProduct.Ingredients.ReplaceEntityCollection(myNewIngredients, ((Ingredient i) => i.Id));

This seems to have resolved that issue!

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 23 March 2019 · 1 min read · 154 words

Part of AskGif Blog · coding

You might also like

How to Replace child collections in Entity Framework? | AskGif Blog