- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 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);
我收到以下错误:
Missing type map configuration or unsupported mapping.
Mapping types: EntityQueryable
1 -> PreferenceVM
1[[Fwims.Core.Data.Model.Setting.Service, Fwims.Core.Data.Model, Version=1.0.1.10, Culture=neutral, PublicKeyToken=null]] -> Fwims.Core.ViewModel.Setting.PreferenceVM
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable
看起来我的映射是错误的,我尝试过的都没有用。如何正确地将域对象映射到数据传输对象?
最佳答案
这里
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<TSource>
创建映射.
但是稍后您将尝试映射 IQeryable<TSource>
.虽然 AutoMapper 可以使用基映射来映射派生类,IQueryable<T>
不源自 ICollection<T>
,因此缺少类型映射异常。
解决方案是从 IQueryable<T>
的一些公共(public)基础接口(interface)创建一个映射和 ICollection<T>
,即 IEnumerable<T>
.
所以将上面的替换为:
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
因为没有目标成员)。
关于c# - 将多个来源合并到一个目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43614417/
我在哪里可以从 RFCDES 表中获得 RFCOPTIONS 字符串的描述?每个参数的含义是什么? (H=?,N=?,g=?,l=?, ...) 最佳答案 我认为您应该在交易 sm59 中看到: ht
我在导航组件架构中使用底部导航。我想动态更改第一个 fragment 目标。所以我有 Fragment1 和 Fragment2。我想在某些情况下,单击底部导航栏中的第一项打开 Fragment1,在
有没有使用非硬编码 RFC 目的地的好方法? 现在我们的解决方案是检查使用的是哪个系统,然后将目的地分配给一个变量 IF cl_role EQ 'P'. p_dest = 'ESW300'
我能够使目标沿着屏幕顶部随机生成并在屏幕底部消失。唯一的问题是目标每次只能移动到一个固定点。我希望它们移动到屏幕底部的随机点。有任何想法吗?谢谢。 这是代码: -(void)addTarget{ CC
我有一个列表,其中包含推送 View 的行。该 View 有另一个列表,它推送另一个 View 。原来的List,第一个推送的List会随着数据的变化而更新。但是,推送时最后一个 View 不会更新。
我目前正在致力于将 IBM Webshere 应用程序迁移到 Spring Boot。 作为其中的一部分,有一个 MDB 类需要转换为 @JmsListener。该 MDB 有一个监听多个队列的方法。
我需要使用 phpmailer 发送存储在 mysql 数据库中的多封电子邮件,但是当我想执行它时,它返回 500 页错误,我明白问题出在我在其中输入数据时: ->setTo(["'".$row['e
我正在准备环游世界,并且很想知道世界各地的顶级景点有哪些,因此我正在尝试抓取某个地方的顶级目的地。我想最终到达一个国家的顶级地方以及他们最好的景点。最近添加了 Google Destinations
我想用箭头从当前用户位置旋转 ImageView 到以前保存的某个 GPS 位置。我该怎么做? 从 Location.distanceBetween 方法中获取的 Initial Bearing 是否
我希望能够从 application.properties 设置 @JMSlistener 目标 我的代码是这样的 @Service public class ListenerService {
我有一个 View “B”,它有一个带有参数的初始化程序。 struct B: View { let arg: Int init(arg: Int) { self.ar
好的,我们开始吧。我有一个 cocos2d 应用程序,并且有一些目标朝玩家移动。当玩家移动时,我希望他们再次慢慢地将目的地更改为玩家,这样他们就不会只是移动到空旷的空间中。是否可以更改 Sprite
我正在将 OnDestinationChangedListener 附加到我的 Activity 中的 NavController。 它适用于所有 fragment 目标。但是,当我导航到 Activ
我正在通过制作一个定向的表格对我们的表格进行一些 FK 分析 表示 FK 依赖关系的图,然后遍历 图形。在我的代码中,我使用有向图命名所有内容 术语,但我想多说一点 报告中的“用户友好”。 在这种情况
在 SSIS 包中,我将平面文件中的数据导入数据库,执行 sql 查询并将结果导出到 excel 目的地。我的问题是我有一个 convert,cast 语句,它在 sql 中很好,它返回一个值为 HH
我有一个 Spring 应用程序,它需要查找 JMS 目标才能完成工作。根据我们处于开发环境还是系统测试环境,我们将使用不同的 JMS 实现。 (Oracle AQ在系统测试中使用WebLogic,A
我有一个包含三列(原点、目的地、距离)的矩阵,我想用 Pandas 将其转换为原点/目的地矩阵,有没有一种快速方法(lambda、map)可以在不使用 for 循环的情况下执行此操作? 例如(我拥有的
拥有原始的 websocket 实现: @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocke
自从切换到 Storyboard后,我通过以下方式加载了一个 View Controller [self performSegueWithIdentifier:@"identifier" sender
我有一个名为“detailViewController”的 UIViewController。 使用推送转场通过多个不同的转场访问此 View Controller 。 我面临的问题是 detailV
我是一名优秀的程序员,十分优秀!