2013-02-22 54 views
1

我有两种复杂类型:一种进入服务层,作为ViewModel和其他存储库层。 他们被定义为如下:用于自定义复杂类型的AutoMapper

//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(); 

其基本上没有自定义类型的列表。我不明白的地方是哪里错了: Uptill现在我发现是:

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

我该如何解决?我想从库中的实体映射到我的视图模型

回答

1

你真的是这一行:

Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>(); 

还是你想圆创建地图的其他方式?

Mapper.CreateMap<ProductDetailsEntity, ProductDetailsModel>(); 

我不知道要映射哪个方向,但如果你确实希望你将不得不从SettingDetailsModel定义映射回SettingDetails前者,那就是:

Mapper.CreateMap<SettingDetails, SettingDetailsModel>(); 
+0

我希望它从Repository映射到ViewModels。对不起,万一不清楚。 – bhuvin 2013-02-22 13:10:37

+0

是否要从ProductDetailsModel映射到ProductDetailsEntity?如果是的话,请在我的帖子中按照建议。如果不是,那么交换你的第三个映射语句。 – wal 2013-02-22 13:11:56

+0

令人惊讶的是我的哑...... – bhuvin 2013-02-22 13:20:39

相关问题