c# - 使用 AutoMapper 获取异常

标签 c# automapper

我正在对一种方法进行单元测试,该方法使用自动映射器将我的域中的类映射到 linq to sql 类。粗略地说,类和映射如下(SupplierEligibilityAllocated 是 L2S 自动生成的类)。

public class SupplierEligibilityTransactionByQuantity
{
    public decimal Eligibility { get; private set; }
    public decimal CoreValue { get; private set; }
    public int? TransactionId { get; private set; }
    public SupplierTransactionStatus Status { get; private set; }
    public int? DebitId { get; set; }
    public int ManifestId { get; private set; }
}

public partial class SupplierEligibilityAllocated
{
 private int _SupplierEligibilityCreditId;
 private int _ManifestId;
 private System.Nullable<int> _QuantityApplied;
 private System.Nullable<decimal> _AmountApplied;
 private System.Nullable<decimal> _CoresReservedByAmount;
 private System.DateTime _InsertDate;
 private EntityRef<Manifest> _Manifest;
 private EntityRef<SupplierEligibilityCredit> _SupplierEligibilityCredit;
}

private static void Map_SupplierEligibilityTransactionByQuantity_To_SupplierEligibilityAllocated()
{
    Mapper.CreateMap<EligibilityTransactionByQuantity, SupplierEligibilityAllocated>()
        .ForMember(dest => dest.SupplierEligibilityCreditId, opt => opt.MapFrom(src => src.TransactionId))
        .ForMember(dest => dest.ManifestId, opt => opt.MapFrom(src => src.ManifestId))
        .ForMember(dest => dest.QuantityApplied, opt => opt.MapFrom(src => Convert.ToInt32(src.Eligibility)))
        .ForMember(dest => dest.AmountApplied, opt => opt.Ignore())
        .ForMember(dest => dest.CoresReservedByAmount, opt => opt.Ignore())
        .ForMember(dest => dest.InsertDate, opt => opt.MapFrom(src => DateTime.UtcNow))
        .ForMember(dest => dest.Manifest, opt => opt.Ignore())
        .ForMember(dest => dest.SupplierEligibilityCredit, opt => opt.Ignore());
}

但是,当该方法执行映射时,它会抛出以下异常。

Trying to map SupplierEligibilityTransactionByQuantity to SupplierEligibilityAllocated.
Missing type map configuration or unsupported mapping.
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.

at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
at AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType)
at AutoMapper.MappingEngine.Map[TSource,TDestination](TSource source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)  

我在测试之前验证了我正在创建映射,并且我毫无问题地调用了 Mapper.AssertConfigurationIsValid()。我也手动进行了映射,没有任何问题。有人知道是什么原因造成的吗?

最佳答案

看来您在调用 Mapper.CreateMap 时指定了错误的类型

尝试做如下的事情:

Mapper.CreateMap<SupplierEligibilityTransactionByQuantity, SupplierEligibilityAllocated>()

关于c# - 使用 AutoMapper 获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1942691/

相关文章:

dependency-injection - AutoMapper TypeConverter 自定义构造函数

Automapper 8-表达式树lambda不得包含空传播运算符

.net - 如何配置 Automapper 以注入(inject) Ninject 2.0?

Automapper 将 DataTable 映射到 POCO 列表

c# - 如何访问5xxxx Modbus地址

c# - 带有正则表达式的 JToken.SelectToken 用于查找与模式匹配的值?

c# - 我应该将什么传递给 SQLitePCL.raw.SetProvider() 以避免 "The type initializer for ' Microsoft.Data.Sqlite.SqliteConnection' 引发异常”

c# - 可以将私有(private)方法放在我的 Controller 中,还是应该使用 asp.net mvc 将它们分离到某种类型的帮助程序类中?

c# - AutoMapper 从多个来源转换

c# - VB.NET 和 C# 之间的二进制移位差异