AutoMapper configuration. ViewModel to DomainModel

时间:2016-05-17 11:20:19

标签: c#

I need litle help with mapping my viewmodel to domain model.

View model:

public class CampaignViewModel
{
    public CampaignVM Campaign { get; set; }
    public DiscountVM Discount { get; set; }
}

Domain Model:

 public class Campaign : Entity
{
    public Campaign();

    public Discount Discount { get; set; }
    // other properties 
}

And how I can handle that via automapper? I tried:

 var config = new MapperConfiguration(cfg => cfg.CreateMap<CampaignViewModel, Campaign>()
            .ForMember(dest => dest, opt => opt.MapFrom(src => src.Campaign)));

But how to map the other classes? Like DiscountVM => Discount...

1 个答案:

答案 0 :(得分:1)

试试这个:

install.packages(path_to_file, repos = NULL, type="source")

或仅仅是这个:

var config = new MapperConfiguration(cfg =>cfg.CreateMap()
.ForMember(dest => dest.Campaign, opt => opt.MapFrom(src => src.Campaign)))
.ForMember(dest => dest.Discount , opt => opt.MapFrom(src => src.Discount )));
var result=Mapper.Map();

因为源类和目标类的属性具有相同的名称。因此,它们将自动映射。你只需要为具有不同名称的属性编写(.ForMember)部分。