我具有以下域模型(一个类):
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";
}
和许多视图模型类,每个视图模型类都可以映射到该领域类:
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; }
地图规则:
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>();
它工作正常。 但是现在我有了以下视图模型类:
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; }
}
和地图规则:
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEditVM>();
但是当我尝试将域对象映射到DriverEditVM时:
var driver = _driverService.GetDriver(id.Value);
DriverEditVM model = mapper.Map<DriverEditVM>(driver);
我的个人信息,就业等属性为空。如何映射?
答案 0 :(得分:0)
在为构成DriverEditVM
一部分的所有其他视图模型创建地图之后,您应该能够做到这一点:
CreateMap<Domain.POCO.Driver.DriverDomain, DriverEditVM>()
.ForAllMembers(opt => opt.MapFrom(src => src));
修改
由于某些成员没有被映射,因此有两种方法,显式地忽略未映射的成员或显式地映射映射的成员:
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));
或
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());
答案 1 :(得分:-1)
解决了这个问题:
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))
;