简体   繁体   English

Automapper问题,似乎忽略了属性映射

[英]Automapper issue, seems to ignore mapping of property

I'm using Automapper version 6.2.2 and I'm having issues with mapping between two classes. 我正在使用6.2.2版的Automapper,并且在两个类之间进行映射时遇到问题。 Automapper is used in the project for all "mapping purposes" and works well for all cases except this one. Automapper在项目中用于所有“映射目的”,并适用于除此以外的所有情况。

public class WeeklyVolume    
{    
    public DateTime Week    
}    

public class WeeklyVolumeViewModel    
{    
    public DateTime Date    
    public string Week    
}`

The mapping between the classes looks like: 类之间的映射如下:

CreateMap<WeeklyVolumeViewModel, WeeklyVolume>()
        .ForMember(dest => dest.Week, opt => opt.MapFrom(src => src.Date))

CreateMap<WeeklyVolume, WeeklyVolumeViewModel>()
        .ForMember(dest => dest.Week, opt => opt.Ignore())
        .ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.Week));

When this mapping is used 使用此映射时

Mapper.Map<IList<WeeklyVolumeViewModel>, IList<WeeklyVolume>>(weeklyVolumes));

an exception is thrown: 引发异常:

Message: AutoMapper.AutoMapperMappingException : Error mapping types. 消息:AutoMapper.AutoMapperMappingException:错误映射类型。

Mapping types: 映射类型:

  • System.FormatException : v. 44 (0) is not a valid value for DateTime. System.FormatException:v。44(0)对于DateTime无效。
  • System.FormatException : The string was not recognized as a valid System.FormatException:该字符串未被识别为有效
  • DateTime. 约会时间。 There is an unknown word starting at index 0. 从索引0开始有一个未知单词。

In the mapping, the string property Week from WeeklyVolumeviewModel is ignored. 在映射中,忽略WeeklyVolumeviewModel中的字符串属性Week。 Though its value "v. 44 (0)" (formatted string with week number) still seems to be trying to get mapped to the WeeklyVolume Week property, which is what I'm guessing causes the error. 尽管其值“ v。44(0)”(带有星期编号的格式化字符串)似乎仍试图映射到WeeklyVolume Week属性,这是我猜测的原因。 All the other mappings between classes work great, but here it does not seem to function properly, or am I missing something? 类之间的所有其他映射工作都很好,但是在这里它似乎无法正常运行,还是我错过了一些东西?

All the mappings are set through using 所有的映射都是通过使用

Mapper.Initialize(cfg => cfg.Addprofile(new DummyMapperclass());

The first thing I would change is 我要改变的第一件事是

public class WeeklyVolume    
{    
    public DateTime Week {get; set;}
}    

public class WeeklyVolumeViewModel    
{    
    public DateTime Date {get; set;}
    public string Week   {get; set;}
}

then, let's see on the field names: you have two fields with the same name, but with the different type. 然后,让我们看一下字段名称:您有两个名称相同但类型不同的字段。 This may confuse Automapper. 这可能会使Automapper混淆。 He will try to convert the fields. 他将尝试转换字段。

You can try to change 'Week' on WeeklyVolume to 'Date' and remove all mappings 'Date' to 'Date' -> Automapper will do all things automatically. 您可以尝试将WeeklyVolume上的“周”更改为“日期”,并删除所有从“日期”到“日期”的映射-> Automapper会自动执行所有操作。

OR You can try to change field name Week (one of these) to say, Week2. 或您可以尝试将字段名称“星期”(其中之一)更改为“ Week2”。

Let me know the results. 让我知道结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM