以下是类和解析器,为什么会出现这个错误?我看不出为什么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);
我做错了什么?这种方法是在映射过程中处理简单转换的最佳方式吗?
谢谢!