简体   繁体   English

比较和插入/更新/删除实体框架中的子级

[英]Comparing and insert/update/delete children in Entity Framework

I have a multiple selection form that needs to be saved into the database. 我有一个选择表格,需要保存到数据库中。 Users can edit their selections on their subsequent visits. 用户可以在随后的访问中编辑其选择。 The items are defined in a table with unique IDs so the form only pass back a list of IDs. 这些项目是在具有唯一ID的表中定义的,因此该表格仅返回ID列表。

It is something like: 就像这样:

Fruits:
    { id=1, name="apple" }
    { id=2, name="orange" }
    { id=3, name="banana" }

People:
    { id=1, name="user", fruits=apple,orange }

In DB there is a link table linking the id of people and id of fruits. 在数据库中,有一个链接表,链接人的ID和水果的ID。

Now when I receive a request to edit, I'll need to compare with existing fruits so I know whether I need to add or delete an entry. 现在,当我收到一个编辑请求时,我需要与现有水果进行比较,以便知道是否需要添加或删除条目。

foreach (var fruit in existing_fruits)
{
    if (post_fruits.Where(e => e.id == fruit.id).Count() == 0) user.Fruits.Remove(fruit);
}

foreach (var fruit in post_fruits)
{
    if (existing_fruits.Where(e => e.id == fruit.id).Count() == 0)
    {
        var entity = context.Fruit.Where(e => e.id == fruit.id);
        user.Fruits.Add(entity);
    }
}

As you can see there are multiple loops and multiple which call on the lists, which makes me wonder if there is a cleaner way in doing this? 如您所见,列表上有多个循环和多个调用,这使我想知道是否有更清洁的方法?

What if you use .Any method here (although it still uses two loops but it is more efficient) : 如果在这里使用.Any方法会怎样(尽管它仍然使用两个循环,但效率更高):

foreach (var fruit in existing_fruits)
    if (!post_fruits.Any(e => e.id == fruit.id)) user.Fruits.Remove(fruit);

foreach (var fruit in post_fruits)
{
    if (existing_fruits.Any(e => e.id == fruit.id)) continue;
    var entity = context.FirstOrDefault(e => e.id == fruit.id);
    if(entity != null) user.Fruits.Add(entity);
}

But it's better to change DB architecture to: 但是最好将数据库体系结构更改为:

Fruits:
    { id=1, name="apple" }
    { id=2, name="orange" }
    { id=3, name="banana" }

People:
    { id=1, name="user" }

PeopleFruits
    { id=1, fruitId = 1, personId = 1, isSelected = 0}

All you need to update records now is to get this PeopleFruits entities of some person. 现在只需更新记录就是获取某人的此PeopleFruits实体。

PeopleFruits[] personPplFruits = cont.PeopleFruits.Where(pf => pf.personId == 1).ToArray();

And update .isSelected properties according to what user has selected. 并根据用户选择的内容更新.isSelected属性。

Check this article please : https://www.simple-talk.com/sql/database-administration/how-to-get-database-design-horribly-wrong/ 请检查这篇文章: https : //www.simple-talk.com/sql/database-administration/how-to-get-database-design-horribly-wrong/

There are a lot of useful functions in EntityFramework.Extended . EntityFramework.Extended中有很多有用的功能。 It contains Batch Update and Delete feature which can be useful in your situation, it also eliminates the need to retrieve and load an entity before modifying it, so it can increase your performance in future. 它包含批处理更新和删除功能,该功能在您遇到的情况中很有用,还消除了在修改实体之前检索和加载实体的需求,因此将来可以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM