1

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

    //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 以及 SettingDetails 为:

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.CreateMap<SettingDetails, SettingDetailsModel>();
Mapper.CreateMap<SideStoneDetails, SideStoneDetailsModel>();
Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>();
Mapper.AssertConfigurationIsValid();

基本上它的自定义类型列表失败了。我不明白哪里出了问题:到目前为止我发现的是:

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

我该如何解决?我想从 REPOSITORY Entity 映射到我的 VIEWMODEL

4

1 回答 1

1

你真的是说这一行吗:

Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>();

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

Mapper.CreateMap<ProductDetailsEntity, ProductDetailsModel>();

我不确定您要映射哪个方向,但如果您确实想要前者,您将不得不定义一个从SettingDetailsModelback 到的地图SettingDetails,即:

Mapper.CreateMap<SettingDetails, SettingDetailsModel>();
于 2013-02-22T13:03:25.297 回答