当我从DTO构建EF对象时,如何将EF对象层次结构保存到db?

时间:2021-08-08 19:35:19

I have created an editor that uses Entity Framework for db-access. The editor is a clientside editor that works via JavaScript. When the user clicks the save-button JavaScript calls my ASP.NET MVC method with the data as a JSON-string. The data is an object containing a list of nodes. I use inheritance in EF to allow the nodes to be of different types. The JSON that is sent from the browser is converted by MVC automatically to a DTO object. I then create EF-objects based on the DTO object. My question is how do I save the modifications to all the objects?

我创建了一个使用Entity Framework进行数据库访问的编辑器。编辑器是一个通过JavaScript工作的客户端编辑器。当用户单击保存按钮时,JavaScript将数据作为JSON字符串调用我的ASP.NET MVC方法。数据是包含节点列表的对象。我在EF中使用继承来允许节点具有不同的类型。从浏览器发送的JSON由MVC自动转换为DTO对象。然后,我根据DTO对象创建EF对象。我的问题是如何保存对所有对象的修改?

Right now I do the following:

现在我做以下事情:

// Convert my DTO object to a EF db object
Document dbDoc = dtoDoc.ToEFDocument();

using (MyEntities db = new MyEntities())
{
    db.ApplyCurrentValues("Documents", doc);
    db.SaveChanges();
}

This works but it only updates the properties in the document object (not the changes that was made in the nodes collection).

这有效,但它只更新文档对象中的属性(而不是更新在nodes集合中进行的更改)。

Is there a simple solution for this?

有一个简单的解决方案吗?

1 个解决方案

#1


0  

Your DTOs must also track information about changes an user did. You must track:

您的DTO还必须跟踪有关用户所做更改的信息。你必须跟踪:

  • Which entities were updated
  • 哪些实体已更新

  • Which entities were removed
  • 删除了哪些实体

  • Which entities were added
  • 添加了哪些实体

  • Which independent associations (many-to-many or one-to-many without mapped FK) were added
  • 添加了哪些独立关联(多对多或一对多而没有映射FK)

  • Which independent associations were removed
  • 哪些独立协会被删除

Once you get this information on the server you must use it to correctly set state of every entity you want to save to database. There is no automatic mechanism for doing this in EF and ApplyCurrentValues only works for single entity (not for whole entity graph).

在服务器上获取此信息后,必须使用它来正确设置要保存到数据库的每个实体的状态。在EF中没有自动机制,ApplyCurrentValues仅适用于单个实体(不适用于整个实体图)。

Doing this without information about changes usually requires loading all modified data from database and update them from DTOs instead of creating disconnected entities from DTO.

如果没有有关更改的信息,通常需要从数据库加载所有已修改的数据并从DTO更新它们,而不是从DTO创建断开连接的实体。

#1


0  

Your DTOs must also track information about changes an user did. You must track:

您的DTO还必须跟踪有关用户所做更改的信息。你必须跟踪:

  • Which entities were updated
  • 哪些实体已更新

  • Which entities were removed
  • 删除了哪些实体

  • Which entities were added
  • 添加了哪些实体

  • Which independent associations (many-to-many or one-to-many without mapped FK) were added
  • 添加了哪些独立关联(多对多或一对多而没有映射FK)

  • Which independent associations were removed
  • 哪些独立协会被删除

Once you get this information on the server you must use it to correctly set state of every entity you want to save to database. There is no automatic mechanism for doing this in EF and ApplyCurrentValues only works for single entity (not for whole entity graph).

在服务器上获取此信息后,必须使用它来正确设置要保存到数据库的每个实体的状态。在EF中没有自动机制,ApplyCurrentValues仅适用于单个实体(不适用于整个实体图)。

Doing this without information about changes usually requires loading all modified data from database and update them from DTOs instead of creating disconnected entities from DTO.

如果没有有关更改的信息,通常需要从数据库加载所有已修改的数据并从DTO更新它们,而不是从DTO创建断开连接的实体。