[英]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.MyEntityViewModelDto -> Models.MyEntity
Type Map configuration:Map型配置:
ViewModelDto -> ViewModelDto -> Models.MyEntityViewModelDto -> 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 scaffolding在
MyEntity
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.