添加,删除和更新相关实体 [英] Adding, removing and updating related entities

查看:103
本文介绍了添加,删除和更新相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简体,我有一个实体框架映射两个表对象:项目属性。每个项目都有一定的属性(一对多)。

Simplified, I have an Entity Framework mapping two tables to objects: Items and Properties. Each item has certain properties (one to many).

这是我的计划之外,我收到物业死的项目,这是新项目或现有项目的更新,他们的属性。这些数据可以从一个WCF呼叫,Web表单POST,反序列化:关键是我要插入和更新项目和属性与我收到未链接数据的数据库

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();
}

您看,还有的对象的特定属性的各种引用( existingItem.Properties p.Name == existingProp.Name p.ItemID == existingItem。 ID ),但它是可行的构建这个方法的更宽泛的版本,甚至可能拨弄在一个小的递归(如果一个属性本身具有其他实体的引用)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆