Automapper无法转换对象类型

时间:2015-12-29 23:43:50

标签: c# automapper

我试图使用automapper并获得异常。 这是我的课程:

public class Image : BaseEntity, IEntity<Guid>
{
    [Key, ForeignKey("Photo")]
    public Guid Id { get; set; }
    public byte[] ImageData { get; set; }
    public string ImageMimeType { get; set; }
    public virtual Photo Photo { get; set; }
}

[MessageContract]
public class ImageDownloadResponse
{
    public ImageDownloadResponse(ImageMetaData metaData, Stream stream)
    {
        DownloadedImageMetadata = metaData;
        ImageByteStream = stream;
    }

    [MessageHeader(MustUnderstand = true)]
    public ImageMetaData DownloadedImageMetadata;
    [MessageBodyMember(Order = 1)]
    public Stream ImageByteStream;
}

[DataContract]
public class ImageMetaData
{
    public ImageMetaData(Guid imageId, string imageMimeType)
    {
        ImageId = imageId;
        ImageMimeType = imageMimeType;
    }
    [DataMember(Name = "ImageId", Order = 0, IsRequired = true)]
    public Guid ImageId;
    [DataMember(Name = "ImageMimeType", Order = 1, IsRequired = true)]
    public string ImageMimeType;
}

这些类的自动配置:

Mapper.CreateMap<Image, ImageDownloadResponse>()
     .ForCtorParam("metaData", opt => opt.MapFrom(src => new ImageMetaData(src.Id, src.ImageMimeType)))
     .ForCtorParam("stream", opt => opt.MapFrom(src => new MemoryStream(src.ImageData)))
     .ForSourceMember(src => src.Id, opt => opt.Ignore())
     .ForSourceMember(src => src.Photo, opt => opt.Ignore());

例外情况是,它无法从System.Guid转换为我的实体图片。

但我没有尝试转换这个字段。我把忽略选项。发生了什么事?

1 个答案:

答案 0 :(得分:2)

尝试使用final,如下所示:

ConstructUsing