c# – AutoMapperMappingException – 缺少类型映射配置或不支持的映射

前端之家收集整理的这篇文章主要介绍了c# – AutoMapperMappingException – 缺少类型映射配置或不支持的映射前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我在我的代码中实现IReadOnlyList时,我的单元测试抛出了AutoMapperMappingException.

从挖掘文章和文档,我的猜测是AutoMapper需要对只读类型进行特殊编码.这会是什么样的?

注意:我尝试了Mapper.AssertConfigurationIsValid();正如另一篇文章所建议,但没有任何改进

Test Name: CreateOrder_ValidContract_CreatesNewOrder Test
FullName: ACME.Maintenance.Domain.Test.OrderServiceTest.CreateOrder_ValidContract_CreatesNewOrder
Test Source: C:\Users\me\documents\visual studio
2015\Projects\ACME.Maintenance\ACME.Maintenance.Domain.Test\OrderServiceTest.cs
: line 65 Test Outcome: Failed Test Duration: 0:00:00.0233941

Result StackTrace: at lambda_method(Closure,ContractDto,Contract
,ResolutionContext ) at
ACME.Maintenance.Domain.ContractService.GetById(String contractId) in
C:\Users\me\documents\visual studio
2015\Projects\ACME.Maintenance\ACME.Maintenance.Domain\ContractService.cs:line
34 at
ACME.Maintenance.Domain.Test.OrderServiceTest.CreateOrder_ValidContract_CreatesNewOrder()
in C:\Users\me\documents\visual studio
2015\Projects\ACME.Maintenance\ACME.Maintenance.Domain.Test\OrderServiceTest.cs:line
69 Result Message: Test method
ACME.Maintenance.Domain.Test.OrderServiceTest.CreateOrder_ValidContract_CreatesNewOrder
threw exception: AutoMapper.AutoMapperMappingException: Missing type
map configuration or unsupported mapping.

Mapping types: ContractDto -> Contract

ACME.Maintenance.Domain.DTO.ContractDto -> ACME.Maintenance.Domain.Contract

Mapper.Initialize(cfg => cfg.CreateMap<ContractDto,Contract>());
Mapper.Initialize(cfg => cfg.CreateMap<PartDto,Part>());
[TestMethod]
public void CreateOrder_ValidContract_CreatesNewOrder()
    {
        //Arrange
        var orderService = new OrderService();
        var contractService = new ContractService(_contractRepository);
        var contract = contractService.GetById(ValidContractId);

        // Act
        var newOrder = orderService.CreateOrder(contract);

        // Assert
        Assert.IsInstanceOfType(newOrder,typeof(Order));

        Guid guidOut;
        Assert.IsTrue(Guid.TryParse(newOrder.OrderId,out guidOut));

        Assert.AreEqual(newOrder.Status,ContractStatus.New);
        Assert.IsInstanceOfType(newOrder.Items,typeof(IReadOnlyList<OrderItem>));

    }

ContractService类:

public Contract GetById(string contractId)
    {
        var contractDto = _contractRepository.GetById(contractId);
        var contract = Mapper.Map<ContractDto,Contract>(contractDto);
        Mapper.AssertConfigurationIsValid();
        return contract;
    }

合同类:

public class Contract
{
    public string ContractId { get; set; }
    public DateTime ExpirationDate { get; set; }

}

ContractDto类:

public class ContractDto
{
    public string ContractId { get; set; }
    public DateTime ExpirationDate { get; set; }

}

解决方法

你的问题是你多次调用Mapping.Initialize:它不是附加的,所以你要覆盖以前的映射,这就是你将地图从ContractDto丢失到Contract的原因

因此,在测试的Setup方法中,您应该像这样调用它:

Mapper.Initialize(cfg => {
     cfg.CreateMap<ContractDto,Contract>();
     cfg.CreateMap<PartDto,Part>();
});
原文链接:https://www.f2er.com/csharp/239098.html

猜你在找的C#相关文章