2017-09-27 2 views
2

소스 클래스에 추상지도 : 내가 쓴 한- 추상적 인

public class ApplicationDriverDomain 
{ 
    public List<ApplicationDriverEquipmentAbstractDomain> Equipments { get; set; } 
} 

public abstract class ApplicationDriverEquipmentAbstractDomain 
{ 
    public int Id { get; set; } 
    public string Make { get; set; } 
    public string Model { get; set; } 
    public string Year { get; set; } 
    public string PlateNumber { get; set; } 
    public string CurrentMileage { get; set; } 
    public string Type { get; protected set; } 
} 

public class ApplicationDriverEquipmentTractorDomain : ApplicationDriverEquipmentAbstractDomain 
{ 
    public ApplicationDriverEquipmentTractorDomain() 
    { 
     Type = ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor; 
    } 
    public string VINNumber { get; set; } 
} 

public class ApplicationDriverEquipmentTrailerDomain : ApplicationDriverEquipmentAbstractDomain 
{ 
    public ApplicationDriverEquipmentTrailerDomain() 
    { 
     Type = ApplicationDriverEquipmentTypeStaticStringsDomain.Trailer; 
    } 

    public string Length { get; set; } 
} 

public class ApplicationDriverEquipmentStraightTruckDomain : ApplicationDriverEquipmentAbstractDomain 
{ 
    public ApplicationDriverEquipmentStraightTruckDomain() 
    { 
     Type = ApplicationDriverEquipmentTypeStaticStringsDomain.StraightTruck; 
    } 

    public string VINNumber { get; set; } 
    public string Length { get; set; } 
} 

public class ApplicationDriverEquipmentCargoVanDomain : ApplicationDriverEquipmentAbstractDomain 
{ 
    public ApplicationDriverEquipmentCargoVanDomain() 
    { 
     Type = ApplicationDriverEquipmentTypeStaticStringsDomain.CargoVan; 
    } 

    public string VINNumber { get; set; } 
    public string Length { get; set; } 
} 

:

public abstract class ApplicationDriverEquipmentFormAbstractVM 
{ 
    [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 PlateNumber { get; set; } 
    [StringLength(256)] 
    public string CurrentMileage { get; set; } 
} 

public class ApplicationDriverEquipmentTractorFormVM : ApplicationDriverEquipmentFormAbstractVM 
{ 
    [StringLength(256)] 
    public string VINNumber { get; set; } 
} 

public class ApplicationDriverEquipmentTrailerFormVM : ApplicationDriverEquipmentFormAbstractVM 
{ 
    [StringLength(256)] 
    public string Length { get; set; } 
} 

public class ApplicationDriverEquipmentStraightTruckFormVM : ApplicationDriverEquipmentFormAbstractVM 
{ 
    [StringLength(256)] 
    public string VINNumber { get; set; } 
    [StringLength(256)] 
    public string Length { get; set; } 
} 

public class ApplicationDriverEquipmentCargoVanFormVM : ApplicationDriverEquipmentFormAbstractVM 
{ 
    [StringLength(256)] 
    public string VINNumber { get; set; } 
    [StringLength(256)] 
    public string Length { get; set; } 
} 


public class ApplicationDriverFormVM 
{ 
    public ApplicationDriverEquipmentTractorFormVM EquipmentTractor { get; set; } 
    public ApplicationDriverEquipmentTrailerFormVM EquipmentTrailer { get; set; } 
    public ApplicationDriverEquipmentStraightTruckFormVM EquipmentStraightTruck { get; set; } 
    public ApplicationDriverEquipmentCargoVanFormVM EquipmentCargoVan { get; set; } 

} 

다음 나는 다음과 같은 방법을 설명 대상 클래스의 장비 속성에 매핑 할 다음 자동 작업자 규칙 :

 CreateMap<ViewModels.ApplicationDriverEquipmentFormAbstractVM, ApplicationDriverEquipmentAbstractDomain>(); 

     CreateMap<ViewModels.ApplicationDriverEquipmentTractorFormVM, ApplicationDriverEquipmentTractorDomain>(); 
     CreateMap<ViewModels.ApplicationDriverEquipmentTrailerFormVM, ApplicationDriverEquipmentTrailerDomain>(); 
     CreateMap<ViewModels.ApplicationDriverEquipmentStraightTruckFormVM, ApplicationDriverEquipmentStraightTruckDomain>(); 
     CreateMap<ViewModels.ApplicationDriverEquipmentCargoVanFormVM, ApplicationDriverEquipmentCargoVanDomain>(); 

     CreateMap<ViewModels.ApplicationDriverFormVM, ApplicationDriverDomain>() 
      .ForMember(dest => dest.Equipments, opt => opt.MapFrom(src => new List<ViewModels.ApplicationDriverEquipmentFormAbstractVM>() { src.EquipmentCargoVan, src.EquipmentStraightTruck, src.EquipmentTractor, src.EquipmentTrailer })); 

그러나 작동하지 않습니다. 왜?

+1

그래서 오류 받고있는 또는 무엇이거나 당신이해야한다고 생각한다는 일이되지 않는 이유는 무엇입니까? –

답변

2

해결책을 찾았습니다. 매핑 상속을 선언 할 필요가 : 그것에 대해

 CreateMap<ViewModels.ApplicationDriverEquipmentFormAbstractVM, ApplicationDriverEquipmentAbstractDomain>() 
      .Include<ViewModels.ApplicationDriverEquipmentTractorFormVM, ApplicationDriverEquipmentTractorDomain>() 
      .Include<ViewModels.ApplicationDriverEquipmentTrailerFormVM, ApplicationDriverEquipmentTrailerDomain>() 
      .Include<ViewModels.ApplicationDriverEquipmentStraightTruckFormVM, ApplicationDriverEquipmentStraightTruckDomain>() 
      .Include<ViewModels.ApplicationDriverEquipmentCargoVanFormVM, ApplicationDriverEquipmentCargoVanDomain>() 
      ; 

문서 :

https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance

관련 문제