5

I have inherited my base "User" class to create role specific types.

eg...

public class Business : User{
 public string BusinessName;
}

I've done a similar thing for my View Models, starting with a basic "UserModel" and inheriting that to include role specific functionality.

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

When I use Automapper to map changes to my BusinessModel back to my Business object, it doesn't 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 to any fields on the "Business" it's self are present. However any changes to fields inherited from User aren't.

Is Automapper just unable to map inherited types like this? Or am I missing something?

4

1 回答 1

6

您需要为基本类型创建映射,然后包含继承的类型。请参阅此处的自动映射器文档。

Mapper.CreateMap<UserModel, User>()
      .Include<BusinessModel, Business>();
Mapper.CreateMap<BusinessModel, Business>();
于 2013-05-27T19:31:23.387 回答