C# Automapper将2个类配置为一个
我有以下数据库基础结构类:C# Automapper将2个类配置为一个,c#,automapper,automapper-6,C#,Automapper,Automapper 6,我有以下数据库基础结构类: [Table("ApplicationDriverEquipments")] public partial class ApplicationDriverEquipment { public int Id { get; set; } [StringLength(256)] public string Make { get; set; } [StringLength(256)] public string Model { get;
[Table("ApplicationDriverEquipments")]
public partial class ApplicationDriverEquipment
{
public int Id { get; set; }
[StringLength(256)]
public string Make { get; set; }
[StringLength(256)]
public string Model { get; set; }
[StringLength(256)]
public string Year { get; set; }
[StringLength(256)]
public string VINNumber { get; set; }
[StringLength(256)]
public string PlateNumber { get; set; }
[StringLength(256)]
public string CurrentMileage { get; set; }
[StringLength(256)]
public string Length { get; set; }
public int TypeId { get; set; }
public virtual ApplicationDriverEquipmentType Type { get; set; }
public int DriverId { get; set; }
public virtual ApplicationDriver Driver { get; set; }
}
[Table("ApplicationDriverEquipmentTypes")]
public partial class ApplicationDriverEquipmentType
{
public ApplicationDriverEquipmentType()
{
Equipments = new HashSet<ApplicationDriverEquipment>();
}
public int Id { get; set; }
[Required]
[StringLength(256)]
public string Name { get; set; }
public virtual ICollection<ApplicationDriverEquipment> Equipments { get; set; }
}
我编写了以下自动映射规则来解决此问题:
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentTractorDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.ForMember(c => c.Type.Name, p => p.UseValue<string>(Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor));
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentTrailerDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.ForMember(c => c.Type.Name, p => p.UseValue<string>(Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.Trailer));
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentStraightTruckDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.ForMember(c => c.Type.Name, p => p.UseValue<string>(Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.StraightTruck));
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentCargoVanDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.ForMember(c => c.Type.Name, p => p.UseValue<string>(Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.CargoVan));
我有一个错误:
表达式“c=>c.Type.Name”必须解析为顶级成员,而不是
任何子对象的属性。在子类型上使用自定义解析程序
或者改用AfterMap选项
更新
我重写了地图:
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentTractorDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.AfterMap((src, dest)=> dest.Type.Name = Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor);
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentTrailerDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.AfterMap((src, dest) => dest.Type.Name = Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.Trailer);
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentStraightTruckDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.AfterMap((src, dest) => dest.Type.Name = Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.StraightTruck);
CreateMap<Domain.POCO.Application.ApplicationDriverEquipmentCargoVanDomain, Infrastructure.Asset.ApplicationDriverEquipment>()
.AfterMap((src, dest) => dest.Type.Name = Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.CargoVan);
但现在我犯了一个错误:
类型映射配置:ApplicationDriverEquipmentTractor域->
应用河流设备
Domain.POCO.Application.ApplicationDriverEquipmentTractorDomain->
Infrastructure.Asset.ApplicationDriver设备
属性:类型-->AutoMapper.AutoMappingException:缺少
键入映射配置或不支持的映射
映射类型:
字符串->应用程序驱动设备类型
System.String->Infrastructure.Asset.ApplicationDriverEquipmentType
似乎,我不明白如何正确映射它请尝试使用MapFrom方法:
.ForMember(c => c.Type.Name, p => p.MapFrom(s => Domain.StaticStrings.ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor));
您正在尝试从中映射
ApplicationDriverEquipmentTractorDomain。类型是字符串
到
ApplicationDriver设备。类型是ApplicationDriver设备类型
您的映射配置在哪里
甚至可以将字符串映射到ApplicationDriverEquipmentType吗?
当然,你可以有一个字符串名,但是你从哪里得到Id和设备呢
我怀疑您不希望每次映射时都创建该类型的新实例,而是需要从某种字典中查找实例,类似于
要实现这个想法,您只需要
从数据库加载所有ApplicationDriverEquipmentType
假设名称是唯一的,将它们放入字典中
注册一个或多个文件如下
实现这一点的一种方法是使用
你可以用像这样的东西
void convertingusingfunc映射函数;
并放入您自己的函数,该函数将按名称解析ApplicationDriverEquipmentType,假设名称是唯一的,如下所示:
var applicationEquipments = new ApplicationDriverEquipmentTypeRepository().FindAll(); // get all the values somehow from db
var dictionary = applicationEquipments.ToDictionary(x=>x.Name);
Func<string, ApplicationDriverEquipmentType> resolver = x=>dictionary[x];
另一种方法是使用
本质上,这个想法是相同的-预加载对象的映射,只是插入它的方式不同类型是类中的字符串,它有名称属性吗?@ironstone13,类型是ApplicationDriverEquipmentAbstractDomain类的字符串,但有ApplicationDriverEquipmentType是ApplicationDriverEquipmentHey,@Oleh-Sh,很抱歉我的回答冗长重复,但我终于写下了,请看下面我的回答。如果你有任何问题,请告诉我know@OlegSh为什么要在映射逻辑中设置这样的类型c=>c.Type.Name。就我所见,它是一个简单的字符串属性,所以它必须是c=>c。Type@raimondBurgaj,如您所见,类型的类型为ApplicationDriverEquipmentType,而不是目标类中的字符串:
var applicationEquipments = new ApplicationDriverEquipmentTypeRepository().FindAll(); // get all the values somehow from db
var dictionary = applicationEquipments.ToDictionary(x=>x.Name);
Func<string, ApplicationDriverEquipmentType> resolver = x=>dictionary[x];