【发布时间】:2022-01-03 15:40:44
【问题描述】:
我想使用 AutoMapper 将带有嵌套列表的 EntityDto 映射到实体,然后使用 SaveChanges() 调用对其进行更新。
问题在于 AutoMapper 将嵌套的 List 元素映射为新对象,因此 EntityFramework 认为我想添加具有现有 Id 的新对象。
例子:
public class Entity
{
public Guid Id { get; set; }
public List<NestedEntity> NestedEntityList { get; set; }
}
public class EntityDto
{
public Guid Id { get; set; }
public List<NestedEntityDto> NestedEntityList { get; set; }
}
public class NestedEntity
{
public Guid Id { get; set; }
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
}
public class NestedEntityDto
{
public Guid Id { get; set; }
public string PropertyTwo { get; set; }
}
例如,实体有一个包含 2 个 NestedEntity 对象的列表
{
"Id": "EntityId"
"NestedEntityList": [
{
"Id": "A",
"PropertyOne": "Value A",
"PropertyTwo": "Value AA"
},
{
"Id": "B",
"PropertyOne": "Value B",
"PropertyTwo": "Value BB"
}
]
}
更新:(A修改,B删除,C添加)
EntityDto 有一个包含 2 个 NestedEntity 对象的列表
{
"Id": "EntityId"
"NestedEntityList": [
{
"Id": "A",
"PropertyTwo": "Value AAA (Updated)"
},
{
"Id": "C",
"PropertyTwo": "Value CC"
}
]
}
无需进一步配置 AutoMapper 通过创建新对象来映射 NestedEntityList。这会导致两个问题:
- EntityFramework 会将这些新对象作为新创建的对象而不是已更新的现有对象进行跟踪。这会导致以下错误消息:“无法跟踪实体类型“NestedEntity”的实例,因为已经在跟踪另一个具有键值“A”的实例”。
- 如果NestedEntity 有PropertyOne 值,那么映射后为null,因为NestedEntityDto 没有PropertyOne。我想更新 EntityDto(即 PropertyTwo)中的属性并保留其他所有内容的值。
所以我想要达到的结果:(A修改,B删除,C添加)
{
"Id": "EntityId"
"NestedEntityList": [
{
"Id": "A",
"PropertyOne": "Value A", //Old value, not updated with NULL
"PropertyTwo": "Value AAA (Updated)" //Updated value
},
{
"Id": "C", //New item added in the update
"PropertyOne": NULL,
"PropertyTwo": "Value CC"
}
]
}
我需要如何配置 AutoMapper 来实现这一点?有可能吗?
【问题讨论】:
-
研究
AutoMapper.Collection. -
这解决了我的问题,谢谢。您应该写下您的评论作为答案,以便我接受。
标签: .net entity-framework automapper