Automapper 8映射无法正常工作 [英] Automapper 8 mapping not working properly

查看:321
本文介绍了Automapper 8映射无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Automapper ForMember方法映射不同名称的不同属性时,我有两个模型类.它在不同属性的映射上引发自动映射器配置验证异常.

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.

我已经尝试了很多,但是并没有帮助.我不知道为什么当我尝试将数量属性与Quntity属性映射时,它引发了异常.但是当我在两个模型类中都输入相同名称的属性时,它将起作用

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.

请问我如何解决问题?

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

            });
        }
    }

以下是异常详细信息

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

推荐答案

您使用的是Profile错误,您的个人资料应如下所示:

Your profile should look like:

 public class ProductModelMapConfigurator :  Profile, IMapConfigurator
    {
        public ProductModelMapConfigurator()
        {

                CreateMap<StandardizeInventory.Models.Product.ProductModel, Models.ProductModel>()
                //.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => src.Brand.Name))
                .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>();

        }
    }

从您的Profile内部摆脱该Mapper.Initialize调用,并更改该配置文件以使用构造函数,而不管该Configure方法如何.当名称匹配时,您也不需要MapFrom,即"AutoMapper"的"Auto".

Get rid of that Mapper.Initialize call from inside your Profile, and change the profile to use a constructor, not whatever that Configure method is. You also don't need MapFrom when the names match, that's the "Auto" of "AutoMapper".

这篇关于Automapper 8映射无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆