0

我遵循了关于存储库和工作单元模式设计的教程,它也包括在内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

4

1 回答 1

0

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

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

如果您在使用 AutoMapper 时遇到困难,请考虑编写自己的映射代码。您将对正在发生的事情有更清晰的了解。

于 2016-09-19T15:57:49.740 回答