自定义复杂类型的AutoMapper



我有两种复杂类型:一种是在作为ViewModel的服务层,另一种在Repository层。它们的定义如下:

    //The Repository Layer 
public class ProductDetailsEntity
        {
            public Int64 StockNumber { get; set; }
            public String StockName { get; set; }
            public String Image { get; set; }
            public Decimal Price { get; set; }
            public String JewelleryName { get; set; }
            public String ShortDescription { get; set; }
            public Int64 ShippingDays { get; set; }
            public String DesignCode { get; set; }
            public List<SettingDetails> SettingsDetails { get; set; }
            public List<SideStoneDetails> SideStoneDetails { get; set; }
        }
 // The Service Layer 
public class ProductDetailsModel
    {
        public Int64 StockNumber { get; set; }
        public String StockName { get; set; }
        public String Image { get; set; }
        public Decimal Price { get; set; }
        public String JewelleryName { get; set; }
        public String ShortDescription { get; set; }
        public Int64 ShippingDays { get; set; }
        public String DesignCode { get; set; }
        public List<SettingDetailsModel> SettingsDetails { get; set; }
        public List<SideStoneDetailsModel> SideStoneDetails { get; set; }
    }

将SettingsDetailsModel和SettingsDetails设置为:

public class SettingDetails // same Structure with different Names
    {
        public Int64 AttributeId { get; set; }
        public String AttributeName { get; set; }
        public String AttributeValue { get; set; }
    }

SideStoneDetailsModel和SideStoneDetails为:

public class SideStoneDetailsModel
    {
        public Int64 SideStoneSettingId { get; set; }
        public String SideStoneSettingName { get; set; }
        public String SideStoneSettingValue { get; set; }
    }

现在,当从实体映射到模型时,它抛出了一个AutoMapper异常,声明:

The following property on Repository.Entities.SettingDetails cannot be mapped: 
SettingsDetails
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Service.Models.SettingDetailsModel.
Context:
Mapping to property SettingsDetails of type Repository.Entities.SettingDetails from source type Service.Models.SettingDetailsModel
Mapping to property SettingsDetails of type System.Collections.Generic.List`1[[Repository.Entities.SettingDetails, Repository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] from source type System.Collections.Generic.List`1[[Service.Models.SettingDetailsModel, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping to type Repository.Entities.ProductDetailsEntity from source type Service.Models.ProductDetailsModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.

现在,Mapper实现包含

Mapper.CreateMap<SettingDetails, SettingDetailsModel>();
Mapper.CreateMap<SideStoneDetails, SideStoneDetailsModel>();
Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>();
Mapper.AssertConfigurationIsValid();

基本上它对于自定义类型的列表是失败的。我不明白哪里出了问题:Uptill现在我发现的是:

  • 为不同的类型添加单独的映射。检查
  • 自定义映射器函数-但为什么?在这种情况下,我不明白为什么要这样做

如何解决此问题?我想从REPOSITORY实体映射到我的VIEWMODEL

你真的是指这行吗:

Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>();

还是你想反过来创建地图?

Mapper.CreateMap<ProductDetailsEntity, ProductDetailsModel>();

我不确定你想映射哪个方向,但如果你确实想要前者,你就必须定义一个从SettingDetailsModelSettingDetails的映射,即:

Mapper.CreateMap<SettingDetails, SettingDetailsModel>();

相关内容

  • 没有找到相关文章

最新更新