【问题标题】:Merge multiple sources into a single destination将多个来源合并到一个目的地
【发布时间】:2017-09-22 16:32:15
【问题描述】:

我想使用 AutoMapper 将 2 个域对象组合成一个数据传输对象。

领域模型:

public class Service  {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<DownloadService> DownloadServices { get; set; } = new HashSet<DownloadService>();
}

public class DownloadService {
    public int Id { get; set; }
    public int PageLimit { get; set; }
    public virtual int ServiceId { get; set; }
    public virtual Service Service { get; set; }
}

public class Volume {
    public override int Id { get; set; }
    public bool IsActive { get; set; }
    public string Path { get; set; }
    public string Description { get; set; }
}

DTO:

 public class PreferenceVM {
        public ICollection<VolumeVM> Volumes { get; set; }
        public ICollection<ServiceVM> Services { get; set; }
 }

 public class ServiceVM {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public ICollection<DownloadServiceVM> DownloadServices { get; set; } = new HashSet<DownloadServiceVM>();
 }

public class DownloadServiceVM {
        public int Id { get; set; }
        public int PageLimit { get; set; }
        public int CleaningInterval { get; set; }
}

 public class VolumeVM {
        public int Id { get; set; }
        public bool IsActive { get; set; }
        public string Path { get; set; }
        public string Description { get; set; }
}

映射:

 cfg.CreateMap<Volume, VolumeVM>().ReverseMap();
 cfg.CreateMap<DownloadService, DownloadServiceVM>().ReverseMap();
 cfg.CreateMap<Service, ServiceVM>()
     .ForMember(d => d.DownloadServices, opt => opt.MapFrom(s => s.DownloadServices))
     .ReverseMap();

 cfg.CreateMap<ICollection<Volume>, PreferenceVM>()
            .ForMember(x => x.Volumes, y => y.MapFrom(src => src)).ReverseMap();
 cfg.CreateMap<ICollection<Service>, PreferenceVM>()
            .ForMember(x => x.Services, y => y.MapFrom(src => src)).ReverseMap();

当我尝试上面的映射时:

 var services = serviceRepository.GetAll();
 var volumes = volumeRepository.GetAll();

 var entities = mapper.Map<PreferenceVM>(services);
 entities = mapper.Map(volumes, entities);

我收到以下错误:

缺少类型映射配置或不支持的映射。

映射类型:EntityQueryable1 -> PreferenceVM Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1[[Fwims.Core.Data.Model.Setting.Service, Fwims.Core.Data.Model,版本=1.0.1.10,文化=中性, PublicKeyToken=null]] -> Fwims.Core.ViewModel.Setting.PreferenceVM

看来我的映射是错误的,我尝试过的都没有奏效。如何正确地将域对象映射到数据传输对象?

【问题讨论】:

    标签: c# asp.net-mvc entity-framework automapper entity-framework-core


    【解决方案1】:

    这里

    cfg.CreateMap<ICollection<Volume>, PreferenceVM>()
        .ForMember(x => x.Volumes, y => y.MapFrom(src => src)).ReverseMap();
    

    cfg.CreateMap<ICollection<Service>, PreferenceVM>()
        .ForMember(x => x.Services, y => y.MapFrom(src => src)).ReverseMap();
    

    您从ICollection&lt;TSource&gt; 创建映射。

    但是稍后您尝试映射IQeryable&lt;TSource&gt;。虽然 AutoMapper 可以使用基映射来映射派生类,但 IQueryable&lt;T&gt; 不是从 ICollection&lt;T&gt; 派生的,因此缺少类型映射异常。

    解决方案是从IQueryable&lt;T&gt;ICollection&lt;T&gt; 的一些通用基本接口创建一个映射,即IEnumerable&lt;T&gt;

    所以将上面的替换为:

    cfg.CreateMap<IEnumerable<Volume>, PreferenceVM>()
        .ForMember(x => x.Volumes, y => y.MapFrom(src => src));
    cfg.CreateMap<IEnumerable<Service>, PreferenceVM>()
        .ForMember(x => x.Services, y => y.MapFrom(src => src));
    

    并且当前的问题将得到解决。

    请注意,ReverseMap 在这种情况下不起作用,所以我刚刚删除了它。如果您需要此类功能,则必须手动创建该映射(最终使用 ConvertUsing,因为没有目标成员)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多