私はリポジトリと作業単位パターンのチュートリアルに続き、それにはAutoMapper
も含まれています。このチュートリアルでは古いバージョンを使用していましたが、AutoMapperConfiguration
クラスとそのプロファイルを少し修正する必要がありましたが、現在はMissing type map configuration or unsupported mapping
というエラーが表示されます。AutoMapperの問題 - タイプマップの設定が欠落しているか、サポートされていないマッピング
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);
}
インデックスActionMethod