Simplified, I have an Entity Framework mapping two tables to objects: Items
and Properties
. Each item has certain properties (one to many).
From outside my program, I receive "dead" items with properties, which are new items or updates of existing items, with their properties. This data could be from an WCF call, a web form POST, a deserialization: the point is I want to insert and update items and properties in the database with unlinked data I receive.
I found various related questions and answers (of which not all even compile). The problem is I have to write loads of code to synchronize the properties of an existing item and an incoming, updated item:
private static void UpdateProperties(Item existingItem, Item updatedItem, TestdatabaseEntities context)
{
// Find deleted properties
foreach (var existingProp in existingItem.Properties.ToList()) // ToList() to work on a local copy, otherwise you'll be removing items from an enumeration
{
var inUpdate = updatedItem.Properties.Where(p => p.Name == existingProp.Name).FirstOrDefault();
if (inUpdate == null)
{
// Property with this Name was not found as property in the updated item, delete it
context.Properties.DeleteObject(existingProp);
}
}
// Find added or updated properties
foreach (var updatedProp in updatedItem.Properties)
{
var inDatabase = existingItem.Properties.Where(p => p.ItemID == existingItem.ID && p.Name == updatedProp.Name).FirstOrDefault();
if (inDatabase == null)
{
// Added
inDatabase = new Property { Name = updatedProp.Name };
existingItem.Properties.Add(inDatabase);
}
// Updated ( & added), map properties (could be done with something like AutoMapper)
inDatabase.Value = updatedProp.Value;
// etc...
}
context.SaveChanges();
}
You see, there are various references to specific properties of the objects (existingItem.Properties
, p.Name == existingProp.Name
, p.ItemID == existingItem.ID
), but it will be doable to build a more generic version of this method, and maybe even fiddle in a little recursion (what if a Property
itself has references to other entities?).
However, I was wondering: can this (whole process, or parts of it) be done more easily? And no, I cannot delete all Properties from an Item and re-add them upon an update, because there's other data in those entities I want to preserve.