【问题标题】:AutoMapper - Missing type map configuration System.ObjectAutoMapper - 缺少类型映射配置 System.Object
【发布时间】:2021-09-25 23:12:04
【问题描述】:

我已经在Startup.cs 的 EF Core 中设置了 AutoMapper,如下所示:

    services.AddAutoMapper(
        typeof(EventItemEstablishmentProfile), 
        typeof(EventItemProfile),
        typeof(GroceryItemEstablishmentProfile), 
        typeof(GroceryItemProfile),
        typeof(GroceryStoreItemEstablishmentProfile), 
        typeof(GroceryStoreItemProfile),
        typeof(RestaurantItemEstablishmentProfile), 
        typeof(RestaurantItemProfile),
        typeof(MenuItemEstablishmentProfile), 
        typeof(MenuItemProfile));

我有这些 AutoMapper 配置文件:

namespace Vepo.Domain
{
    public class GroceryItemProfile : Profile
    {
        public GroceryItemProfile()
        {
            CreateMap<GroceryItem, GroceryItemDto>();
            CreateMap<GroceryItemDto, GroceryItem>();
        }
    }
}

namespace Vepo.Domain
{
    public class GroceryItemEstablishmentProfile : Profile
    {
        public GroceryItemEstablishmentProfile()
        {
            CreateMap<GroceryItemEstablishment, GroceryItemEstablishmentDto>();
            CreateMap<GroceryItemEstablishmentDto, GroceryItemEstablishment>();
        }
    }
}

GroceryItemEstablishment 包含一个名为 VeganItemGroceryItem 类型字段,因此我们在这里讨论的是嵌套映射。

当需要像这样在代码中使用它时:

public async override Task<TVeganItemEstablishmentDto> Insert(TVeganItemEstablishmentDto entity)
{
    var toReturnVeganItem = entity.VeganItem;
    var toReturnEstablishment = entity.Establishment;
    var x = mapper.Map<TVeganItem>(entity);

我得到错误:

AutoMapper.AutoMapperMappingException:缺少类型映射配置 或不受支持的映射。

映射类型:对象 -> GroceryItem System.Object -> Vepo.Domain.GroceryItem

为什么错误说完全涉及类型Object

调试代码我们可以看到类型不是Object:

【问题讨论】:

    标签: c# automapper asp.net5


    【解决方案1】:

    您正在尝试将TVeganItemEstablishmentDto 映射到TVeganItem。我无法从代码本身判断,但我这相当于将GroceryItemEstablishmentDto 映射到GroceryItem。您的个人资料中不存在该映射。您的意思是将entity.VeganItem 属性映射为输入吗?

    【讨论】:

    • 是的,哎呀!愚蠢的错误。映射类型和提供的实体不匹配。正如您所建议的,var x = mapper.Map&lt;TVeganItem&gt;(entity.VeganItem); 确实有效。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-11-17
    • 2018-11-15
    • 2019-03-01
    • 2017-04-16
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多