Automapper 8 映射无法正常工作

Automapper 8 mapping not working properly

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

我已经尝试了很多,但它没有 help.I 不知道为什么当我尝试将数量 属性 映射到数量 属性 时抛出异常。但是当我在两个模型 类 中输入相同的 属性 名称时,它就可以工作

下面是所有模型 类、关于自动映射器的异常和配置。

你能帮我解决一下吗?

 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 

如有任何帮助,我们将不胜感激。谢谢

你用的 Profile 错了,see the documentation on Profiles

您的个人资料应该如下所示:

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

        }
    }

从您的个人资料中删除 Mapper.Initialize 调用,并更改个人资料以使用构造函数,而不是 Configure 方法。当名称匹配时,您也不需要 MapFrom,即 "AutoMapper" 的 "Auto"。