首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动地图摘要

自动地图摘要
EN

Stack Overflow用户
提问于 2017-09-27 19:44:46
回答 1查看 2.2K关注 0票数 4

来源类别:

代码语言:javascript
复制
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; }

}

然后,我想将它映射到目的类中的Equipments属性,它描述了以下方式:

代码语言:javascript
复制
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; }
}

我编写了以下自动控制规则:

代码语言:javascript
复制
        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 }));

但不起作用。为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-27 20:18:27

我找到了解决办法。需要声明映射继承:

代码语言:javascript
复制
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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46456048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档