我有一些DTO的接口:
public interface IGalleryView
{
ICollection<IGalleryImageView> Images { get; set; }
}
public interface IGalleryImageView
{
// Some simple properties
}
以及以下具体类型:
public class GalleryView : IGalleryView
{
public GalleryView() {
Images = new List<IGalleryImageView>();
}
public ICollection<IGalleryImageView> Images { get; set; }
}
public class GalleryImageView : IGalleryImageView
{
}
这些是从我的EF POCO实体映射的。这些实体看起来像:
public partial class Gallery {
// Constructors removed for brevity
public virtual ICollection<GalleryImage> Images { get; set; }
}
public partial class GalleryImage {
public virtual Gallery Gallery { get; set; }
}
我将这些映射到AutoMapper中,如下所示:
AutoMapper.Mapper.CreateMap<GalleryImage, IGalleryImageView>()
.As<GalleryImageView>();
AutoMapper.Mapper.CreateMap<Gallery, IGalleryView>()
.As<GalleryView>();
但是,我收到以下错误:
无法映射Contracts.IGalleryImageView上的以下属性: 图片 添加自定义映射表达式,忽略,添加自定义解析程序或修改目标类型Contracts.IGalleryImageView。 语境: 映射到属性图像从Model.GalleryImage到Contracts.IGalleryImageView 从System.Collections.Generic.ICollection
1[[Model.GalleryImage, Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.ICollection
映射到属性图像1 [[Contracts.IGalleryImageView,Contracts,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] 从Model.Gallery类型映射到Contracts.IGalleryView 类型&#39; AutoMapper.AutoMapperConfigurationException&#39;的例外情况被扔了。
我不确定这里有什么问题,因为我已经为转换指定了地图。我该如何解决这个问题?
答案 0 :(得分:0)
似乎上面的代码没问题且问题与层次结构无关,但是需要在原始源对象和As
方法中的对象之间创建地图。对于上面的Gallery映射,它将是:
AutoMapper.Mapper.CreateMap<Gallery, GalleryView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<GalleryView, IGalleryViewView>().As<GalleryViewView>();