如何使用 AutoMapper 将 Dto 映射到具有嵌套对象的现有对象实例?
我有以下 Dto 和带有嵌套子实体的实体。
public class Dto
{
public string Property { get; set; }
public string SubProperty { get; set; }
}
public class Entity
{
public string Property { get; set; }
public SubEntity Sub { get; set; }
}
public class SubEntity
{
public string SubProperty { get; set; }
}
如何使用 AutoMapper 设置映射,以便使用 Dto 中的值更新 Entity 的现有实例。
我正在使用 Mapper.Map(dto,entity)
更新现有实体,但是当我尝试将 Dto.SubProperty
映射到 Entity.Sub.SubProperty< /code> 我收到一个异常“必须解析为顶级成员。参数名称:lambdaExpression”。
如果我使用 FromMember
创建从 Dto
到 SubEntity
的映射,则 Entity.Sub
将被替换为新的实例SubEntity
但这不是我想要的。我只是希望它更新 Entity
的 Sub
属性上的 SubEntity
现有实例的属性。
我怎样才能实现这一目标?
I have the following Dto and entity with a nested sub entity.
public class Dto
{
public string Property { get; set; }
public string SubProperty { get; set; }
}
public class Entity
{
public string Property { get; set; }
public SubEntity Sub { get; set; }
}
public class SubEntity
{
public string SubProperty { get; set; }
}
How can I set up a mapping with AutoMapper that will allow me to update an existing instance of Entity with the values from a Dto.
I'm using Mapper.Map(dto, entity)
to update an existing entity but when I try to map Dto.SubProperty
to Entity.Sub.SubProperty
I get an exception for "must resolve to top-level member. Parameter name: lambdaExpression".
If I create a mapping from Dto
to SubEntity
using FromMember
then Entity.Sub
gets replaced with a new instance of SubEntity
but that's not what I want. I just want it to update the properties of the existing instance of SubEntity
on the Sub
property of Entity
.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过结合使用
ResolveUsing()
方法并实现IValueResolver
和ConvertUsing()
方法解决了这个问题,实现ITypeConverter
。我的一些映射场景比正常情况更复杂,包括双向映射、嵌套类和嵌套集合。以上帮助我解决了这些问题。
编辑
根据要求,我提供了一个示例解决方案。这个例子比我处理的实际类型简单得多。
如果您运行使用
MapperConfig
的示例,您将获得以下输出:字符串属性全部按预期更新,但
entity.Sub
被替换为SubEntity
的新实例,当您想要更新将保留到数据库的 ORM 实体时,这没有什么好处。如果您修改
Main
以便使用MapperConfig2
,您仍然会像以前一样更新字符串属性,但是,entity .sub
仍然具有与之前相同的SubEntity
实例。使用MapperConfig2
运行示例会给出以下输出:MapperConfig2
中的主要区别在于ResolveUsing
与MyResolver
一起使用保留entity.Sub
的值。I solved it by using a combination of the
ResolveUsing<T>()
method and implementingIValueResolver
and theConvertUsing<T>()
method and implementingITypeConverter<TSource,TDestination>
.Some of my mapping scenarios are more complicated than normal including bidirectional mapping and nested classes and nested collections. The above helped me to solve them.
EDIT
As requested, I've included an example solution. This example is much simpler than the actual types I was dealing with.
If you run the example, which is using
MapperConfig
, you'll get the following output:The string properties all get updated as one would want them to, but
entity.Sub
gets replaced with a new instance ofSubEntity
which is no good for when you are wanting to update entities for an ORM that will be persisted to a database.If you modify
Main
so thatMapperConfig2
is used instead, you'll still have the string properties updated as before, but,entity.sub
still has the same instance ofSubEntity
that it had before. Running the example withMapperConfig2
gives this output:The key difference in
MapperConfig2
is thatResolveUsing
is used along withMyResolver
to preserve the value ofentity.Sub
.