[英]Map fields with same name to different fileds in automapper
I have a model which I am trying to map from Match class in .net core 2.0. 我有一个模型,我试图从.net core 2.0中的Match类映射。 Both the classes have a Name property.
这两个类都有一个Name属性。
I need to map Match.Value => ViewCompany.Name 我需要映射Match.Value => ViewCompany.Name
But it always puts Match.Name into ViewCompany.Name 但它总是将Match.Name放入ViewCompany.Name
Here is my AutomapperProfile : 这是我的AutomapperProfile :
CreateMap<Match, ViewCompany>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Value));
.ForMember(dest => dest.Name , opt => opt.MapFrom(src => src.Value )) .ForMember(dest => dest.Name ,opt => opt.MapFrom(src => src.Value ))
ViewCompany : ViewCompany :
public class ViewCompany
{
public ViewCompany()
{
}
public ViewCompany(string name)
{
this.Name = name;
}
public int Id { get; set; }
public string Name { get; set; }
}
The above mapping doesn't work. 上面的映射不起作用。
But if I change property name in the model to something else like "Value" or "tempName" and update the automapper profile, it works fine. 但是,如果我将模型中的属性名称更改为“Value”或“tempName”之类的其他内容并更新automapper配置文件,则可以正常工作。
So, is it not possible to map properties with same names to different properties in Automapper? 那么,是否无法将具有相同名称的属性映射到Automapper中的不同属性?
What happens here is that Name
is mapped through the constructor . 这里发生的是
Name
通过构造函数映射。 A simple way to avoid that is to tell AM what constructor to use: 避免这种情况的一种简单方法是告诉AM使用什么构造函数:
CreateMap<Match, ViewCompany>().ConstructUsing(source=>new ViewCompany());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.