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