AutoMapper - 缺少类型映射配置 System.Object

问题描述 投票:0回答:1

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

Startup.cs

我有这些 AutoMapper 配置文件:

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

A 
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
类型的字段,称为
GroceryItem
,所以我们在这里讨论的是嵌套映射。
当需要在代码中使用它时,如下所示:

VeganItem

我收到错误:

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

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

为什么错误提示涉及类型
public async override Task<TVeganItemEstablishmentDto> Insert(TVeganItemEstablishmentDto entity) { var toReturnVeganItem = entity.VeganItem; var toReturnEstablishment = entity.Establishment; var x = mapper.Map<TVeganItem>(entity);

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

enter image description here

c# asp.net-core automapper
1个回答
3
投票
Object

映射到

TVeganItemEstablishmentDto
。 我无法从代码本身看出,但我会
猜测
这相当于将TVeganItem映射到
GroceryItemEstablishmentDto
。 您的个人资料中不存在该映射。 您的意思是改为将
GroceryItem
属性映射为输入吗?
    

© www.soinside.com 2019 - 2024. All rights reserved.