AutoMapper解析器未传递预期类型

4

以下是类和解析器,为什么会出现这个错误?我看不出为什么ProductAddModel被传入了。

AutoMapper.AutoMapperMappingException在用户代码中无法处理
信息:提供的值的类型为System.Decimal,但期望的类型为 AuctionCMS.Framework.Models.Admin.ProductAddModel。更改值 解析器源类型,或使用FromMember将源值重定向到 值解析器中提供的值。

类型:

    public class Currency
    {
        public Int64 Value { get; set; }
        // Spot saved for currency type and any other extra properties
    }

   public class Product
    {
        public Currency Price { get; set; }
        public Currency ReservePrice { get; set; }
          }

    public class ProductAddModel
    {
        public Decimal Price { get; set; }
        public Decimal ReservePrice { get; set; }
    }

解析器代码:

public class DecimalToCurrencyValueResolver : ValueResolver<decimal, Currency>
{
    #region Overrides of ValueResolver<decimal,Currency>

    protected override Currency ResolveCore(decimal source)
    {
        return new Currency() { Value = (Int64)((decimal)source) * 1000 };
    }

    #endregion
}

public class CurrencyToDecimalValueResolver : ValueResolver<Currency, decimal>
{
    #region Overrides of ValueResolver<decimal,Currency>

    protected override decimal ResolveCore(Currency source)
    {
        return (decimal)source.Value * 1000;
    }

映射代码:

            Mapper.CreateMap<ProductAddModel, Product>()
                .ForMember(x => x.Price, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>())
                .ForMember(x => x.ReservePrice, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>());
            Mapper.CreateMap<Product, ProductAddModel>()
                .ForMember(x => x.Price, opt => opt.ResolveUsing<CurrencyToDecimalValueResolver>())
                .ForMember(x => x.ReservePrice, opt => opt.ResolveUsing<CurrencyToDecimalValueResolver>());

var model = new ProductAddModel();
var product = new Product();
Mapper.Map<ProductAddModel, Product>(model, product);

我做错了什么?这种方法是在映射过程中处理简单转换的最佳方式吗?
谢谢!
2个回答

3
请使用TypeConverters代替:
    public  class CurrencyToDecimalTypeConverter : ITypeConverter<Currency, Decimal>
    {
        public decimal Convert(ResolutionContext context)
        {
            return ((Currency)context.SourceValue).Value * 1000;
        }
    }

    public class DecimalToCurrencyTypeConverter : ITypeConverter<Decimal, Currency>
    {
        public Currency Convert(ResolutionContext context)
        {
            return new Currency() { Value = (Int64)((decimal)context.SourceValue) * 1000 };
        }
    }

以下是配置信息:

Mapper.CreateMap<ProductAddModel, Product>();
Mapper.CreateMap<Product, ProductAddModel>();
Mapper.CreateMap<Decimal, Currency>().ConvertUsing<DecimalToCurrencyTypeConverter>();
Mapper.CreateMap<Currency, Decimal>().ConvertUsing<CurrencyToDecimalTypeConverter>();

var model = new ProductAddModel();
var product = new Product();
Mapper.Map<ProductAddModel, Product>(model, product);

我基本上得出了相同的结论。谢谢! - rboarman
太好了!正是我解决问题所需要的提示。 - Rod Hartzell

0
在解析器中使用十进制数,在映射中传递productAddModel。所以映射应该是这样的。
Mapper.CreateMap<ProductAddModel, Product>()
                .ForMember(x => x.Price, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>().FromMember(e => e.Value ));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接