Automapper map into nested class

前端 未结 3 375
抹茶落季
抹茶落季 2020-12-14 18:56

I have 1 class that I need t map into multiple classes, for eg.

This is the source that I\'m mapping from(view model):

public class UserBM
{
    publ         


        
3条回答
  •  余生分开走
    2020-12-14 19:35

    I have another solution. The main idea is that AutoMapper know how to flatten nested objects when you name properly properties in flattened object: adding nested object property name as a prefix. For your case Location is prefix:

    public class UserBM
    {
        public int UserId { get; set; }
    
        public int LocationId { get; set; }
        public string LocationAddress { get; set; }
        public string LocationState { get; set; }
        public string LocationCountry { get; set; }
        ...
    }
    

    So creating familiar mapping from nested to flattened and then using ReverseMap method allows AutomMapper to understand how to unflatten nested object.

    CreateMap()
       .ReverseMap();
    

    That's all!

提交回复
热议问题