使用 AutoMapper 映射已映射的接口集合
Mapping already-mapped Interface Collection using AutoMapper
我有以下一些 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>();
但是,我收到以下错误:
The following property on Contracts.IGalleryImageView cannot be mapped:
Images
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Contracts.IGalleryImageView.
Context:
Mapping to property Images from Model.GalleryImage to Contracts.IGalleryImageView
Mapping to property Images from System.Collections.Generic.ICollection1[[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]]
Mapping from type Model.Gallery to Contracts.IGalleryView
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
我不确定这里的问题是什么,因为我已经为转换指定了地图。我该如何解决这个问题?
上面的代码似乎没问题,问题与层级无关,但是需要在原始源对象和As
方法中的对象之间创建一个映射。对于上面的 Gallery 映射,它将是:
AutoMapper.Mapper.CreateMap<Gallery, GalleryView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<GalleryView, IGalleryViewView>().As<GalleryViewView>();
我有以下一些 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>();
但是,我收到以下错误:
The following property on Contracts.IGalleryImageView cannot be mapped: Images Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Contracts.IGalleryImageView. Context: Mapping to property Images from Model.GalleryImage to Contracts.IGalleryImageView Mapping to property Images from 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]] Mapping from type Model.Gallery to Contracts.IGalleryView Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
我不确定这里的问题是什么,因为我已经为转换指定了地图。我该如何解决这个问题?
上面的代码似乎没问题,问题与层级无关,但是需要在原始源对象和As
方法中的对象之间创建一个映射。对于上面的 Gallery 映射,它将是:
AutoMapper.Mapper.CreateMap<Gallery, GalleryView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<GalleryView, IGalleryViewView>().As<GalleryViewView>();