Automapper缺lesstypes映射configuration或不支持的映射?

实体模型

public partial class Categoies { public Categoies() { this.Posts = new HashSet<Posts>(); } public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public Nullable<int> PositionId { get; set; } public virtual CategoryPositions CategoryPositions { get; set; } public virtual ICollection<Posts> Posts { get; set; } } 

查看模型

 public class CategoriesViewModel { public int Id { get; set; } [Required(ErrorMessage = "{0} alanı boş bırakılmamalıdır!")] [Display(Name = "Kategori Adı")] public string Name { get; set; } [Display(Name = "Kategori Açıklama")] public string Description { get; set; } [Display(Name = "Kategori Pozisyon")] [Required(ErrorMessage="{0} alanı boş bırakılmamalıdır!")] public int PositionId { get; set; } } 

CreateMap

 Mapper.CreateMap<CategoriesViewModel, Categoies>() .ForMember(c => c.CategoryPositions, option => option.Ignore()) .ForMember(c => c.Posts, option => option.Ignore()); 

地图

 [HttpPost] public ActionResult _EditCategory(CategoriesViewModel viewModel) { using (NewsCMSEntities entity = new NewsCMSEntities()) { if (ModelState.IsValid) { try { category = entity.Categoies.Find(viewModel.Id); AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category); //category = AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel); //AutoMapper.Mapper.Map(viewModel, category); entity.SaveChanges(); // Veritabanı işlemleri başarılı ise yönlendirilecek sayfayı // belirleyip ajax-post-success fonksiyonuna gönder. return Json(new { url = Url.Action("Index") }); } catch (Exception ex) { } } // Veritabanı işlemleri başarısız ise modeli tekrar gönder. ViewBag.Positions = new SelectList(entity.CategoryPositions.ToList(), "Id", "Name"); return PartialView(viewModel); } } 

和错误

缺lesstypes映射configuration或不支持的映射。 映射types:CategoriesViewModel – > Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D NewsCMS.Areas.Admin.Models.CategoriesViewModel – > System.Data.Entity.DynamicProxies.Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D

目标path:Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D

来源值:NewsCMS.Areas.Admin.Models.CategoriesViewModel

我错过了什么? 我试图find,但我不能看到问题。

UPDATE

我已经在Global.asax的application_start中指定了

 protected void Application_Start() { InitializeAutoMapper.Initialize(); } 

InitializeClass

 public static class InitializeAutoMapper { public static void Initialize() { CreateModelsToViewModels(); CreateViewModelsToModels(); } private static void CreateModelsToViewModels() { Mapper.CreateMap<Categoies, CategoriesViewModel>(); } private static void CreateViewModelsToModels() { Mapper.CreateMap<CategoriesViewModel, Categoies>() .ForMember(c => c.CategoryPositions, option => option.Ignore()) .ForMember(c => c.Posts, option => option.Ignore()); } } 

谢谢。

你在哪里指定了映射代码(CreateMap)? 参考: 我在哪里configurationAutoMapper?

如果在调用Map方法之前没有注册configuration,将会收到Missing type map configuration or unsupported mapping.

注意Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D类的exception? 这是一个entity framework代理。 我build议你处理你的EF上下文,以确保所有的对象都是从数据库中急切地加载的,并且不存在这样的代理:

 [HttpPost] public ActionResult _EditCategory(CategoriesViewModel viewModel) { Categoies category = null; using (var ctx = new MyentityFrameworkContext()) { category = ctx.Categoies.Find(viewModel.Id); } AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category); //category = AutoMapper.Mapper.Map<CategoriesViewModel, Categoies>(viewModel, category); entity.SaveChanges(); } 

如果实体检索在数据访问层内执行(当然这是正确的方法),请确保在从DAL返回实例之前处理您的EF上下文。

在类AutoMapperconfiguration文件中,您需要为您的实体和视图模型创build一个映射。

ViewModel到域模型映射:

这通常在AutoMapper/DomainToViewModelMappingProfile

Configure() ,添加一行

 Mapper.CreateMap<YourEntityViewModel, YourEntity>(); 

域模型到ViewModel映射:

ViewModelToDomainMappingProfile ,添加:

 Mapper.CreateMap<YourEntity, YourEntityViewModel>(); 

要点例子

我这样做是为了消除错误:

 Mapper.CreateMap<FacebookUser, ProspectModel>(); prospect = Mapper.Map(prospectFromDb, prospect); 

检查你的Global.asax.cs文件,并确保这一行在那里

  AutoMapperConfig.Configure(); 

我find了解决scheme,谢谢所有的答复。

 category = (Categoies)AutoMapper.Mapper.Map(viewModel, category, typeof(CategoriesViewModel), typeof(Categoies)); 

但是,我已经不知道原因了。 我完全不能理解。

我知道这是一个相当古老的问题,但我发现正确的解决scheme是我没有声明assembly属性。

我的代码是:

 using AutoMapper; ... namespace [...].Controllers { public class HousingTenureTypesController : LookupController<HousingTenureType, LookupTypeModel> { Mapper.CreateMap<HousingTenureType, LookupTypeModel>().ReverseMap(); } ... } 

这是通过在我的命名空间声明之前添加以下行来解决的:

 [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(HousingTenureTypesController), "AutoMapperStart")] 

完整的代码是:

 using AutoMapper; ... [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(HousingTenureTypesController), "AutoMapperStart")] namespace [...].Controllers { public class HousingTenureTypesController : LookupController<HousingTenureType, LookupTypeModel> { Mapper.CreateMap<HousingTenureType, LookupTypeModel>().ReverseMap(); } ... }