سؤال

I have Two Complex Types : One is into the Service Layer which serves as a ViewModel and other at the Repository Layer. They are defined as given below :

    //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; }
    }

Having SettingsDetailsModel as well as SettingDetails as :

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

        public String AttributeName { get; set; }

        public String AttributeValue { get; set; }

    }

And SideStoneDetailsModel and SideStoneDetails as :

public class SideStoneDetailsModel
    {
        public Int64 SideStoneSettingId { get; set; }

        public String SideStoneSettingName { get; set; }

        public String SideStoneSettingValue { get; set; }
    }

Now, while Mapping From the Entity to a Model , It is Throwing an AutoMapper Exception stating :

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.

Now, The Mapper implementation contains

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

Basically its failing for the Lists of the Custom Type. I dont understand where is going wrong : Uptill now What i have found is :

  • Add Seperate Mappings for the different Types . CHECK !
  • Custom Mapper Functions - But Why ? In this case i cant figure why should it be done ?

How do I Resolve this ? I want to MAP from REPOSITORY Entity to my VIEWMODEL

هل كانت مفيدة؟

المحلول

Did you really mean this line:

Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>();

or did you want to create the map the other way round?

Mapper.CreateMap<ProductDetailsEntity, ProductDetailsModel>();

I'm not sure which direction you want to map but if indeed you do want the former you are going to have to define a map from SettingDetailsModel back to SettingDetails, that is:

Mapper.CreateMap<SettingDetails, SettingDetailsModel>();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top