ASP.NET Core with EF Core - DTO Collection mapping

前端 未结 3 2118
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 04:32

I am trying to use (POST/PUT) a DTO object with a collection of child objects from JavaScript to an ASP.NET Core (Web API) with an EF Core context as my data source.

<
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 05:01

    AutoMapper is the best solution.

    You can do it very easily like this :

        Mapper.CreateMap();
        Mapper.CreateMap();
    
        Mapper.CreateMap();
        Mapper.CreateMap();
    

    Note : Because AutoMapper will automatically map the List to List.since they have same name, and there is already a mapping from Person to PersonDto.

    If you need to know how to inject it to ASP.net core,you have to see this article : Integrating AutoMapper with ASP.NET Core DI

    Auto mapping between DTOs and entities

    Mapping using attributes and extension methods

提交回复
热议问题