none
Automapper 8 mapping not working properly RRS feed

  • Question

  • I have two model classes, when I try to map different properties of different name by using Automapper ForMember method. It throws an automapper configuration validation exception on the mapping of different property.

    I have tried a lot but It does not help.I do not know why It is throwing an exception when I try to map Quantity property with Quntity property. but when I put same name of the property in both the model classes then it works

    Below is located all the model classes, exception and configurations regarding automapper.

    Could you please help me, that how to solve problem?

     public class ProductModel
        {
            public ProductModel()
            {
                Id = GuidContext.Current.NewGuid();
                ProductHistory = new HashSet<ProductHistoryModel>();
            }
    
            public Guid Id { get; set; }
            public string ProductCode { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public bool IsActive { get; set; }
            public decimal? Price { get; set; }
            public int? Quntity { get; set; }
            public Guid ProductCategoryId { get; set; }
            public Guid? BrandId { get; set; }
    
            public Guid ProductAttributeId { get; set; }
    
            public virtual BrandModel Brand { get; set; }
            public virtual ProductCategoryModel ProductCategory { get; set; }
    
            public virtual ProductAttributeModel ProductAttribute { get; set; }
            public virtual ICollection<ProductHistoryModel> ProductHistory { get; set; }
        }
    
    

    The another class is public class ProductModel { public string Name { set; get; } //public List<string> Attributes { set; get; } //public string Brand { get; set; } public decimal? Price { get; set; } public int? Quantity { get; set; } } }

    and the mapping configuration is
    
     public class ProductModelMapConfigurator :  Profile, IMapConfigurator
        {
            public void Configure()
            {
                Mapper.Initialize(cfg =>
                {
                    CreateMap<StandardizeInventory.Models.Product.ProductModel, Models.ProductModel>()
                    //.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => src.Brand.Name))
                    .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                    .ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price))
                    .ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quntity));                
                    //.AfterMap((src, dest) => {
                      //  dest.Attributes = src.ProductAttribute.ProductAttributeValue.Select(x => x.Value).ToList();
                    //});
    
                    CreateMap<Models.ProductModel, StandardizeInventory.Models.Product.ProductModel>();
    
                });
            }
        }
    

    Below is the Exception Details

    AutoMapper.AutoMapperConfigurationException: 
    
    Unmapped members were found. Review the types and members below.
    
    Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
    
    For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
    
    ==========================================================================================
    
    AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
    
    ProductModel -> ProductModel (Destination member list)
    
    StandardizeInventory.Models.Product.ProductModel -> InventoryStoreApi.Models.ProductModel (Destination member list)
    
    
    
    Unmapped properties:
    
    Quantity
    
    
    
       at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) in 
    

    Any help would be appreciated. Thanks

    • Edited by Adldk Monday, February 11, 2019 7:37 AM
    • Moved by CoolDadTx Monday, February 11, 2019 2:47 PM Third party product
    Monday, February 11, 2019 7:35 AM

All replies

  • I have tried a lot but It does not help.I do not know why It is throwing an exception when I try to map Quantity property with Quntity property. but when I put same name of the property in both the model classes then it works

    So what's the problem here, becuase my pass usage of Automapper that's how it works with the property names in both objects must match?

    Monday, February 11, 2019 11:13 AM
  • It is throwing an configuration exception when I put this line in the configuration

    .ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quntity));

    Here I am trying to map Quantity property with Quntity.

    When I put the same name on the both side, so it works.

    Isn't it correct that ForMember method we used to map property with different name?

    Monday, February 11, 2019 12:36 PM
  • Automapper is a third party product. This forum is for C#-specific questions. Please post questions related to the use of Automapper in their forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, February 11, 2019 2:46 PM
  • It is throwing an configuration exception when I put this line in the configuration

    .ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quntity));

    Here I am trying to map Quantity property with Quntity.

    When I put the same name on the both side, so it works.

    Isn't it correct that ForMember method we used to map property with different name?


    Again,  make the correction on the names in the objects where the property name is spelled wrong so that they match. 
    Tuesday, February 12, 2019 9:26 PM