AutoMapper问题 - 缺少类型映射配置或不支持的映射

时间:2016-09-19 13:29:58

标签: asp.net-mvc automapper automapper-5

我遵循了关于存储库和工作单元模式设计的教程,它还包括AutoMapper,我完全没有经验。该教程使用的是旧版本,我必须稍微修改AutoMapperConfiguration类及其配置文件,但现在我收到Missing type map configuration or unsupported mapping错误。

public class Category
{
    public int CategoryID { get; set; }
    public string Name { get; set; }
    public DateTime? DateCreated { get; set; }
    public DateTime? DateUpdated { get; set; }

    public virtual List<Gadget> Gadgets { get; set; }

    public Category()
    {
        DateCreated = DateTime.Now;
    }
}
public class Gadget
{
    public int GadgetID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Image { get; set; }

    public int CategoryID { get; set; }
    public Category Category { get; set; }
}
public class Bootstrapper
{
    public static void Run()
    {            
        //Configure AutoMapper
        AutoMapperConfiguration.Configure();
    }        
}
public static void Configure()
    {

        Mapper.Initialize(cfg => {
            cfg.AddProfile<DomainToViewModelMappingProfile>();
            cfg.AddProfile<ViewModelToDomainMappingProfile>();
        });

        //var config = new MapperConfiguration(cfg => {
        //    cfg.AddProfile<DomainToViewModelMappingProfile>();
        //    cfg.AddProfile<ViewModelToDomainMappingProfile>();
        //});

        //Mapper.Initialize(cfg =>
        //{
        //    cfg.AddProfile<DomainToViewModelMappingProfile>();               

        //    cfg.AddProfile<ViewModelToDomainMappingProfile>();
        //});
    }
public class ViewModelToDomainMappingProfile : Profile
{
    public override string ProfileName
    {
        get { return "ViewModelToDomainMappings"; }
    }

    protected override void Configure()
    {

        //var config = new MapperConfiguration(cfg => cfg.CreateMap<GadgetFormViewModel, Gadget>());
        //var mapper = new Mapper(config);
        //Gadget dto = mapper.Map<Gadget>(GadgetFormViewModel);

        //var config = new MapperConfiguration(cfg => {
        //    cfg.CreateMap<GadgetFormViewModel, Gadget>();
        //});

        //IMapper mapper = config.CreateMapper();
        //var source = new GadgetFormViewModel();
        //mapper.Map<GadgetFormViewModel, Gadget>(source);


        //var gadgetFormViewModel = new GadgetFormViewModel();
        //// Mapper.Configuration.CreateMapper
        //Mapper.Map<GadgetFormViewModel, Gadget>(gadgetFormViewModel);
        //    //// .ForMember(g => g.Name, map => map.MapFrom(vm => vm.GadgetTitle))
        //    //.ForMember(g => g.Description, map => map.MapFrom(vm => vm.GadgetDescription))
        //    //.ForMember(g => g.Price, map => map.MapFrom(vm => vm.GadgetPrice))
        //    //.ForMember(g => g.Image, map => map.MapFrom(vm => vm.File.FileName))

        //    //.ForMember(g => g.CategoryID, map => map.MapFrom(vm => vm.GadgetCategory));

        //Mapper.Map<GadgetFormViewModel, Gadget>(GadgetFormViewModel vm)
        //    .ForMember(g => g.Name, map => map.MapFrom(vm => vm.GadgetTitle))
        //    .ForMember(g => g.Description, map => map.MapFrom(vm => vm.GadgetDescription))
        //    .ForMember(g => g.Price, map => map.MapFrom(vm => vm.GadgetPrice))
        //    .ForMember(g => g.Image, map => map.MapFrom(vm => vm.File.FileName))
        //    .ForMember(g => g.CategoryID, map => map.MapFrom(vm => vm.GadgetCategory));

        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<GadgetFormViewModel, Gadget>()
            .ForMember(g => g.Name, map => map.MapFrom(vm => vm.GadgetTitle))
            .ForMember(g => g.Description, map => map.MapFrom(vm => vm.GadgetDescription))
            .ForMember(g => g.Price, map => map.MapFrom(vm => vm.GadgetPrice))
            .ForMember(g => g.Image, map => map.MapFrom(vm => vm.File.FileName))
            .ForMember(g => g.CategoryID, map => map.MapFrom(vm => vm.GadgetCategory));
        });

        IMapper mapper = config.CreateMapper();
        var source = new GadgetFormViewModel();
        var dest = mapper.Map<GadgetFormViewModel, Gadget>(source);

    }
}
public class DomainToViewModelMappingProfile : Profile
{
    public override string ProfileName
    {
        get { return "DomainToViewModelMappings"; }
    }

    protected override void Configure()
    {
        var configC = new MapperConfiguration(cfg => {
            cfg.CreateMap<Category, CategoryViewModel>();
        });

        IMapper mapperC = configC.CreateMapper();
        var sourceC = new Category();
        mapperC.Map<Category, CategoryViewModel>(sourceC);


        var configG = new MapperConfiguration(cfg => {
            cfg.CreateMap<Gadget, GadgetViewModel>();
        });

        IMapper mapperG = configG.CreateMapper();
        var sourceG = new Gadget();
        mapperG.Map<Gadget, GadgetViewModel>(sourceG);

        //Mapper.Initialize(x => { x.CreateMap<Category, CategoryViewModel>}());
        //var category = new Category();
        //var gadget = new Gadget();
        //Mapper.Map<Category, CategoryViewModel>(category);
        //Mapper.Map<Gadget, GadgetViewModel>(gadget);
    }
}
public ActionResult Index(string category = null)
    {
        IEnumerable<GadgetViewModel> viewModelGadgets;
        IEnumerable<Category> categories;

        categories = categoryService.GetCategories(category).ToList();

        viewModelGadgets = Mapper.Map<IEnumerable<Category>, IEnumerable<GadgetViewModel>>(categories);
        return View(viewModelGadgets);
    }

索引viewModelGadgets = Mapper.Map<IEnumerable<Category>, IEnumerable<GadgetViewModel>>(categories);

ActionMethod行的错误点

1 个答案:

答案 0 :(得分:0)

您确定要从类别映射到GadgetViewModel吗?您没有为该组合定义映射。如果它有效,则需要添加如下内容:

    cfg.CreateMap<Category, GadgetFormViewModel>()
        .ForMember(...)
    );

如果您正在努力使用AutoMapper,请考虑编写自己的映射代码。您将更清楚地了解正在发生的事情。