Automapper在映射时抛出异常

时间:2015-10-16 08:12:13

标签: c# .net wcf model-view-controller automapper

我需要将Dto模型映射到视图模型中,但是在映射自定义字段时,automapper会抛出一个exeption。错误如下:

Frontend.Test.MappingTest.TestAutoMapperMapping threw exception: 
AutoMapper.AutoMapperConfigurationException: The following property on     System.Int32 cannot be mapped: Id
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type System.Int32.
Context:
    Mapping to property Id from Shared.Common.Service.Model.Metadata.FieldDto`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to System.Int32
Mapping from type Common.Service.Model.Dto to BBR.Frontend.Models.ViewModel

我的课程如下:

[DataContract]
public class Dto
{
    [DataMember]
    public FieldDto<int> Id { get; set; }

    [DataMember]
    public FieldDto<string> Name { get; set; }
}


 public class ViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Mapper:

 Mapper.CreateMap<Dto, ViewModel>()
            .ForMember(dest => dest.Id, opts => opts.MapFrom(src =>  src.Id.Value))
            .ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name.Value)).ReverseMap();

任何想法如何处理它?<​​/ p>

FieldDto:

[DataContract]
public class FieldDto<TValue>
{
    [DataMember]
    public TValue Value { get; set; }

    [DataMember]
    public int UserRights { get; set; } = 3;

    [DataMember]
    public int Id { get; set; }
}

我像这样调用ViewModel:

return View(Mapper.Map<ViewModel>(obj));

0 个答案:

没有答案