简体   繁体   English

ASP.NET Core 3.1 Web API 和 Entity Framework Core:AutoMapper 获取错误映射类型 MIssing Type mpa 配置或不支持的映射

[英]ASP.NET Core 3.1 Web API and Entity Framework Core : AutoMapper getting error mapping types MIssing Type mpa configuration or unsupported mapping

I am using Automapper to copy the content from DTO view model to an entity class but I get an error:我正在使用 Automapper 将 DTO 视图 model 中的内容复制到实体 class 但出现错误:

Error mapping types:错误映射类型:
ViewModelDto -> Models.MyEntity ViewModelDto -> Models.MyEntity
Type Map configuration: Map型配置:
ViewModelDto -> ViewModelDto -> Models.MyEntity ViewModelDto -> ViewModelDto -> Models.MyEntity
EmbeddedClass嵌入式类

The last section of the string is an embedded object which is the issue that is throwing the exception.字符串的最后一部分是嵌入式 object,这是引发异常的问题。 It is defined in source and destination class as a collection of records.它在源和目标 class 中定义为记录集合。

In my incoming ViewModelDto, the embedded class is listed as在我传入的 ViewModelDto 中,嵌入式 class 列为

public class ViewModelDto
{
  public List<MyEntity> MyEntities {get;set;}
}

In the MyEntity class, this is autogenerated by scaffoldingMyEntity class 中,这是由脚手架自动生成的

public partial class MyEntity
{
    public MyEntity()
    {
        MyEntities = new HashSet<MyEntity>();
    }

    public virtual ICollection<MyEntity> MyEntities {get;set;}
}

In my MappingProfile.cs :在我的MappingProfile.cs

public MappingProfile()
{
    CreateMap<List<ViewModelDto>, List<MyEntity>>().ReverseMap();
}

When looking at both ViewModelDto and MyEntity class, the MyEntity is defined in both files.查看 ViewModelDto 和 MyEntity class 时,MyEntity 在两个文件中都定义了。 Not sure what is causing the thrown exception不确定是什么导致抛出异常

Tried a few new things, in the mappingprofile.s在mappingprofile.s中尝试了一些新东西

CreateMap<ViewModelDto, Drill>)().ForMember(dest => dest.MyEntity, opt => opt.UseDestination());
CreateMapCreateMap<List<ViewModelDto>, List<MyEntity>>)();

In my startup.cs在我的startup.cs

In the ConfigureServices(IServiceCollection services)
{
  services.AddAutoMapper(typeof(Startup));
}

Seems like there is a Mapping Issue between the List and HashSet, you can try this:好像 List 和 HashSet 之间存在映射问题,你可以试试这个:

CreateMap<ViewModelDto, MyEntity>(MemberList.Source)
    .ForMember(dest => dest.MyEntities, opt => opt.UseDestinationValue());

With this, AutoMapper will try to use the destination value type (HashSet) instead of the origin (List).有了这个,AutoMapper 将尝试使用目标值类型 (HashSet) 而不是原点 (List)。

Added this to my Startup.cs to resolve the issue, Need to register the MappingProfile in the Automapper Service将此添加到我的 Startup.cs 以解决问题,需要在 Automapper 服务中注册 MappingProfile

services.AddAutoMapper(typeof(MappingProfile)); services.AddAutoMapper(typeof(MappingProfile));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 - AutoMapper.AutoMapperMappingException:缺少类型 map 配置或不支持的映射 - ASP.NET Core - AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping Asp.net 核心 3.1 AutoMapperMappingException:缺少类型 map 配置或不支持的映射 - Asp.net core 3.1 AutoMapperMappingException: Missing type map configuration or unsupported mapping .Net Core Automapper 缺少类型 map 配置或不支持的映射 - .Net Core Automapper missing type map configuration or unsupported mapping ASP.net AutoMapper 缺少类型映射配置或不受支持的映射 - ASP.net AutoMapper Missing type map configuration or unsupported mapping Asp.net 核心 AutoMapper 和枚举错误映射类型 - Asp.net Core AutoMapper and Enums Error mapping types 带有Automapper的Web API提供缺少的类型映射配置或不受支持的映射 - Web api with Automapper gives Missing type map configuration or unsupported mapping AutoMapper.AutoMapperMappingException:缺少类型 map 配置或 .NET CORE 中不支持的映射 - AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping in .NET CORE ASP.NET Core Web API &amp; Entity Framework Core; 为什么我收到这个错误? - ASP.NET Core Web API & Entity Framework Core; why am I getting this error? 缺少类型映射配置或不受支持的映射AutoMapper - Missing Type Map Configuration Or Unsupported Mapping AutoMapper Automapper:缺少类型映射配置或不受支持的映射 - Automapper: Missing type map configuration or unsupported mapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM