[英]c# Automapper - How to map object in objetc
I want to mapper these two object,我想映射这两个 object,
public class UpdateCostDto
{
public string JobId { get; set; }
public bool IsSpreadSucceeded { get; set; }
public DateRange CostPeriod { get; set; }
}
public class DateRange
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public class UpdateCost11
{
public string JobId { get; set; }
public bool IsSpreadSucceeded { get; set; }
public DateRange CostPeriod { get; set; }
}
public class DateRange
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
I createMap: CreateMap<UpdateCost11, UpdateCostDto>();我 createMap: CreateMap<UpdateCost11, UpdateCostDto>(); CreateMap<UpdateCostDto, UpdateCost11>();
CreateMap<UpdateCostDto, UpdateCost11>();
but I get an error: "Error mapping types.但我收到一个错误: “错误映射类型。
Mapping types: UpdateCostSpreadStatusMessageDto -> UpdateCostSpreadStatusDto AnalyticsService.Models.IndeedQueueMessage.UpdateCostSpreadStatusMessageDto -> AnalyticsService.Models.UpdateCostSpreadStatusDto映射类型:UpdateCostSpreadStatusMessageDto -> UpdateCostSpreadStatusDto AnalyticsService.Models.IndeedQueueMessage.UpdateCostSpreadStatusMessageDto -> AnalyticsService.Models.UpdateCostSpreadStatusDto
Type Map configuration: UpdateCostSpreadStatusMessageDto -> UpdateCostSpreadStatusDto AnalyticsService.Models.IndeedQueueMessage.UpdateCostSpreadStatusMessageDto -> AnalyticsService.Models.UpdateCostSpreadStatusDto类型 Map 配置:UpdateCostSpreadStatusMessageDto -> UpdateCostSpreadStatusDto AnalyticsService.Models.IndeedQueueMessage.UpdateCostSpreadStatusMessageDto -> AnalyticsService.Models.UpdateCostSpreadStatusDto
Destination Member: CostPeriod "目的地成员:CostPeriod "
You need to add your DateRange
models to your mapper profile.您需要将
DateRange
模型添加到您的映射器配置文件中。
CreateMap<namespace1.DateRange, namespace2.DateRange>().ReverseMap();
Depending on which version you are using you can also use .ReverseMap()
instead of explicitly creating a map for the reverse mapping.根据您使用的版本,您还可以使用
.ReverseMap()
而不是为反向映射显式创建 map。
CreateMap<UpdateCost11, UpdateCostDto>().ReverseMap();
Even though the classes DateRange on Dto and your main class have the same structure, their namespace is different.尽管 Dto 上的类 DateRange 和您的主要 class 具有相同的结构,但它们的命名空间是不同的。 And auto-mapper won't map it as you want.
并且自动映射器不会随心所欲地 map。
You either have to delete one of the DateRange classes and go with one of them or as @Hamlet Hakobyan suggested, you have to add the following configuration您要么必须删除其中一个 DateRange 类和 go ,要么像@Hamlet Hakobyan 建议的那样,您必须添加以下配置
CreateMap<SourceNameSpace.DateRange, DestinationNameSpace.DateRange>();
Change SourceNameSpace and DestinationNameSpace to the one you have.
将 SourceNameSpace 和 DestinationNameSpace 更改为您拥有的那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.