[英]automapper : map one object to many
I have the following domain model (one class): 我有以下域模型(一个类):
public class DriverDomain
{
public int Id { get; set; }
public int CompanyId { get; set; }
public int? TruckId { get; set; }
public int? TrailerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? UnitNo { get; set; }
public int? GpsId { get; set; }
public string CompanyFEIN { get; set; }
public string CompanyName { get; set; }
public string CompanyAddress { get; set; }
public string CompanyCity { get; set; }
public string CompanyZIP { get; set; }
public string CompanyState { get; set; }
public System.DateTime? DOB { get; set; }
public string SSN { get; set; }
public string PhoneNo { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZIP { get; set; }
public string DLno { get; set; }
public string Dlstate { get; set; }
public System.DateTime? DLexp { get; set; }
public System.DateTime? MedExp { get; set; }
public System.DateTime? HireDate { get; set; }
public System.DateTime? TermDate { get; set; }
public bool Active { get; set; }
public bool DrugTest { get; set; }
public string Notes { get; set; }
public string CardNo { get; set; }
public string EmployeeNo { get; set; }
public bool? OwnerOp { get; set; }
public bool OccAcc { get; set; }
public decimal? WeeklyOccAcc { get; set; }
public bool Ifta { get; set; }
public decimal? WeeklyIfta { get; set; }
public bool TrailerRental { get; set; }
public decimal? WeeklyTrailerRent { get; set; }
public bool CargoIns { get; set; }
public decimal? WeeklyCargoIns { get; set; }
public decimal? PilotRebate { get; set; }
public bool OnlineAccess { get; set; }
public int? OnlineId { get; set; }
public bool ViewedSchedule { get; set; }
public int SchedulePriority { get; set; }
public bool Hourly { get; set; }
public decimal? HourlyPay { get; set; }
public string IpassTransponderId { get; set; }
public System.DateTime? RecordDate { get; set; }
public string RecordChangedBy { get; set; }
public string EmgcontactName { get; set; }
public string EmgcontactPhone { get; set; }
public string EmgcontactRelationship { get; set; }
public string Nickname { get; set; }
public string UserId { get; set; }
public string AspNetUserName { get; set; }
public string AvatarUrl { get; set; }
public bool PaidByPercent { get; set; }
public decimal? PercentPaid { get; set; }
public bool PaidByMile { get; set; }
public decimal? PayPerMile { get; set; }
public bool CompanyPlates { get; set; }
public decimal? WeeklyPlateCharge { get; set; }
public bool EnableEscrowDeductionOnPayroll { get; set; }
public decimal WeeklyEscrowDeduction { get; set; }
public bool ShowPersonalConveyance { get; set; } = false;
public bool ShowYardMoves { get; set; } = false;
public string StartTimeOfDay { get; set; } = "00:00:00.000";
}
and many view model classes, each of them can be mapped to this domain class: 和许多视图模型类,每个视图模型类都可以映射到该领域类:
public class DriverPersonalInfoVM
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public System.DateTime? DOB { get; set; }
public string SSN { get; set; }
public string PhoneNo { get; set; }
... ...
public class DriverEmploymentVM
{
public int Id { get; set; }
public System.DateTime? HireDate { get; set; }
public System.DateTime? TermDate { get; set; }
public bool DrugTest { get; set; }
public bool OnlineAccess { get; set; }
public bool ViewedSchedule { get; set; }
map rules: 地图规则:
CreateMap<Domain.POCO.Driver.DriverDomain, DriverPersonalInfoVM>();
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEmploymentVM>();
CreateMap<Domain.POCO.Driver.DriverDomain, DriverPayrollVM>();
CreateMap<Domain.POCO.Driver.DriverDomain, DriverCompensationVM>();
CreateMap<Domain.POCO.Driver.DriverDomain, DriverFuelTollsVM>();
CreateMap<Domain.POCO.Driver.DriverDomain, DriverAvatarVM>();
it works fine. 它工作正常。 But now I have the following view model class: 但是现在我有了以下视图模型类:
public class DriverEditVM
{
public DriverEditVM(int id)
{
Id = id;
PersonalInfo = new DriverPersonalInfoVM { Id = id };
Employment = new DriverEmploymentVM { Id = id };
Payroll = new DriverPayrollVM { Id = id };
Compensation = new DriverCompensationVM { Id = id };
FuelTolls = new DriverFuelTollsVM { Id = id };
Avatar = new DriverAvatarVM { Id = id };
}
public DriverPersonalInfoVM PersonalInfo { get; set; }
public DriverEmploymentVM Employment { get; set; }
public DriverPayrollVM Payroll { get; set; }
public DriverCompensationVM Compensation { get; set; }
public DriverFuelTollsVM FuelTolls { get; set; }
public DriverAvatarVM Avatar { get; set; }
}
and map rule: 和地图规则:
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEditVM>();
but when I try to map domain object to DriverEditVM: 但是当我尝试将域对象映射到DriverEditVM时:
var driver = _driverService.GetDriver(id.Value);
DriverEditVM model = mapper.Map<DriverEditVM>(driver);
I have empty properties PersonalInfo, Employment etc. How to map it? 我有空的属性PersonalInfo,Employment等。如何映射它?
As you have created maps for all your other view models that form part of your DriverEditVM
, you should be able to do this: 在为构成DriverEditVM
一部分的所有其他视图模型创建地图DriverEditVM
,您应该能够执行以下操作:
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEditVM>()
.ForAllMembers(opt => opt.MapFrom(src => src));
Edit 编辑
As some members are not being mapped, there are two approaches, explicitly ignore the un-mapped members or explicitly map the mapped members: 由于未映射某些成员,因此有两种方法,显式忽略未映射的成员或显式映射已映射的成员:
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEditVM>()
.ForMember(dest => dest.IgnoredProperty1, opt => opt.Ignore())
.ForMember(dest => dest.IgnoredProperty2, opt => opt.Ignore())
.ForAllOtherMembers(opt => opt.MapFrom(src => src));
Or 要么
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEditVM>()
.ForMember(dest => dest.PersonalInfo, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Employment, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Payroll, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Compensation, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.FuelTolls, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Avatar, opt => opt.MapFrom(src => src))
.ForAllOtherMembers(opt => opt.Ignore());
Solved this problem : 解决了这个问题:
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEditVM>()
.ForMember(p => p.PersonalInfo, p => p.MapFrom(src => src))
.ForMember(p => p.Avatar, p => p.MapFrom(src => src))
.ForMember(p => p.Compensation, p => p.MapFrom(src => src))
.ForMember(p => p.Employment, p => p.MapFrom(src => src))
.ForMember(p => p.FuelTolls, p => p.MapFrom(src => src))
.ForMember(p => p.Payroll, p => p.MapFrom(src => src))
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.