簡體   English   中英

使用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>();

但是,我收到以下錯誤:

無法映射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[[Model.GalleryImage, Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.ICollection 1 [[Contracts.IGalleryImageView,合同,版本= 1.0.0.0,文化=中性,PublicKeyToken =空]]從類型Model.Gallery映射到Contracts.IGalleryView類型'AutoMapper.AutoMapperConfigurationException'的異常被扔了。

我不確定問題出在哪里,因為我已經為轉換指定了地圖。 我該如何解決?

看來上面的代碼還可以,並且問題與層次結構無關,但是需要在原始源對象和As方法中的對象之間創建一個映射。 對於上面的庫映射,它將是:

AutoMapper.Mapper.CreateMap<Gallery, GalleryView>(); // Additional line here needed
AutoMapper.Mapper.CreateMap<GalleryView, IGalleryViewView>().As<GalleryViewView>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM