C# 映射已使用AutoMapper映射接口集合
对于某些DTO,我有以下接口:C# 映射已使用AutoMapper映射接口集合,c#,entity-framework,automapper,C#,Entity Framework,Automapper,对于某些DTO,我有以下接口: public interface IGalleryView { ICollection<IGalleryImageView> Images { get; set; } } public interface IGalleryImageView { // Some simple properties } 公共接口IGalleryView { i收集图像{get;set;} } 公共接口iGalleryImage视图 { //
public interface IGalleryView
{
ICollection<IGalleryImageView> Images { get; set; }
}
public interface IGalleryImageView
{
// Some simple properties
}
公共接口IGalleryView
{
i收集图像{get;set;}
}
公共接口iGalleryImage视图
{
//一些简单性质
}
以及以下具体类型:
public class GalleryView : IGalleryView
{
public GalleryView() {
Images = new List<IGalleryImageView>();
}
public ICollection<IGalleryImageView> Images { get; set; }
}
public class GalleryImageView : IGalleryImageView
{
}
公共类GalleryView:IGalleryView
{
公共厨房视图(){
图像=新列表();
}
公共ICollection映像{get;set;}
}
公共类GalleryImage视图:iGalleryImage视图
{
}
这些是从我的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; }
}
公共部分类库{
//为简洁起见,删除了构造函数
公共虚拟ICollection映像{get;set;}
}
公共部分类GalleryImage{
公共虚拟库库{get;set;}
}
我正在AutoMapper中映射这些内容,如下所示:
AutoMapper.Mapper.CreateMap<GalleryImage, IGalleryImageView>()
.As<GalleryImageView>();
AutoMapper.Mapper.CreateMap<Gallery, IGalleryView>()
.As<GalleryView>();
AutoMapper.Mapper.CreateMap()
.As();
AutoMapper.Mapper.CreateMap()
.As();
但是,我得到以下错误:
无法映射Contracts.IGalleryImageView上的以下属性:
图像
添加自定义映射表达式、忽略、添加自定义解析程序或修改目标类型Contracts.IGalleryImageView。
背景:
从Model.GalleryImage映射到Contracts.iGalleryImage视图的属性图像
映射到从System.Collections.Generic.ICollection1[[Model.GalleryImage,Model,Version=1.0.0,Culture=neutral,PublicKeyToken=null]]到System.Collections.Generic.ICollection
1[[Contracts.IgalleryImage,Contracts,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]
从type Model.Gallery映射到Contracts.IGalleryView
引发了类型为“AutoMapper.AutoMapperConfigurationException”的异常
我不确定这里的问题是什么,因为我已经为转换指定了映射。如何解决此问题?上述代码似乎没有问题,问题与层次结构无关,但需要在原始源对象和
As
方法中的对象之间创建映射。对于上面的图库映射,它将是:
AutoMapper.Mapper.CreateMap<Gallery, GalleryView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<GalleryView, IGalleryViewView>().As<GalleryViewView>();
AutoMapper.Mapper.CreateMap();//这里需要额外的线路
AutoMapper.Mapper.CreateMap().As();
上述代码似乎没有问题,问题与层次结构无关,但需要在原始源对象和As
方法中的对象之间创建映射。对于上面的图库映射,它将是:
AutoMapper.Mapper.CreateMap<Gallery, GalleryView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<GalleryView, IGalleryViewView>().As<GalleryViewView>();
AutoMapper.Mapper.CreateMap();//这里需要额外的线路
AutoMapper.Mapper.CreateMap().As();
是否缺少任何相关代码?我将上面的代码添加到一个测试项目中,并且没有收到调用Mapper.Map的异常。是否缺少任何相关代码?我将上面的代码添加到一个测试项目中,并且没有得到调用Mapper.Map的异常。