添加,删除和更新相关实体 [英] Adding, removing and updating related entities
问题描述
简化,我有一个实体框架将两个表映射到对象:项目
和属性
。每个项目都具有一定的属性(一对多)。
从我的程序外部,我收到带有属性的死项,这些项目是现有项目的新项目或更新,与他们的属性。这个数据可以来自WCF调用,Web表单POST,反序列化:我想要插入和更新数据库中的项目和属性,而我收到的是未链接的数据。
我发现各种相关问题和答案(其中不包括所有甚至编译)。问题是我必须编写大量的代码来同步现有项目的属性和传入的更新项目:
private static void UpdateProperties(Item existingItem,Item updatedItem,TestdatabaseEntities context)
{
//查找已删除的属性
foreach(在existingItem.Properties.ToList()中的var existingProp)// ToList()在本地副本上工作,否则您将从枚举中删除项目
{
var inUpdate = updatedItem.Properties.Where(p => p.Name == existingProp.Name).FirstOrDefault( );
if(inUpdate == null)
{
//具有此名称的属性未被更新为属性,删除
context.Properties。 DeleteObject的(existingProp);
}
}
//查找添加或更新的属性
foreach(update updatedProp in updatedItem.Properties)
{
var inDatabase = existingItem.Properties.Where(p => p.ItemID == existingItem.ID&& p.Name == updatedProp.Name).FirstOrDefault();
if(inDatabase == null)
{
//添加
inDatabase = new Property {Name = updatedProp.Name};
existingItem.Properties.Add(inDatabase);
}
//更新(&添加),地图属性(可以使用像AutoMapper这样的东西)
inDatabase.Value = updatedProp.Value;
// etc ...
}
context.SaveChanges();
}
您可以看到,对象的特定属性有各种引用( existingItem.Properties
, p.Name == existingProp.Name
, p.ItemID == existingItem。 ID
),但是可以构建一个更通用的这种方法的版本,甚至可能在一点点递归(如果一个属性
但是,我想知道:这个(整个过程,还是其中的一部分)可以更容易地完成吗?不,我不能从项目中删除所有属性,并在更新时重新添加它们,因为我想保留这些实体中的其他数据。
没有全局通用的方法来处理这个代码。您可能会找到一种推广您的样本的方法,但仍将针对特定的案例进行量身定制。您的简单方法包含很多与 Item
和 Property
类紧密耦合的代码。推广此代码将需要在方法之外注入处理这些依赖关系的代理或表达式。
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.
As a developer it is your job to write a code :) and this is not "a lot of code".
There is no global generic way to handle this code. You can probably find a way to generalize your sample but it will still be tailored just for particular set of cases. Your simple method contains a lot of code which is tightly coupled with Item
and Property
class. Generalizing this code would require injecting delegates or expressions handling these dependencies outside of your method.
这篇关于添加,删除和更新相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!