Inherited source and destination of auto-transfer

I inherited my base class "User" to create specific roles.

eg,...

public class Business : User{ public string BusinessName; } 

I did a similar thing for my view models, starting with the base "UserModel" and inheriting this to include role specific features.

 public class BusinessModel : UserModel{ [Required()] public string BusinessName; } 

When I use Automapper to convert changes to my BusinessModel back to my business object, it does not include any changes to the inherited fields.

 //create map between model and business object Mapper.CreateMap<BusinessModel, Business>(); //load the relevant business var business = GetCurrentBusiness(); //map the values across Mapper.Map<BusinessModel, Business>(model, business); 

Changes in any fields in the "Business" he is present. However, any changes to the fields inherited from the user are not.

Can Automapper just not display inherited types? Or am I missing something?

+4
source share
1 answer

You need to create a map for the base type, and then enable the inherited type. See Automapper docs here .

 Mapper.CreateMap<UserModel, User>() .Include<BusinessModel, Business>(); Mapper.CreateMap<BusinessModel, Business>(); 
+5
source

Source: https://habr.com/ru/post/1483017/


All Articles