Ali*_*hşi 64 asp.net-mvc automapper
实体模型
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; }
}
Run Code Online (Sandbox Code Playgroud)
查看模型
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; }
}
Run Code Online (Sandbox Code Playgroud)
CreateMap
Mapper.CreateMap<CategoriesViewModel, Categoies>()
.ForMember(c => c.CategoryPositions, option => option.Ignore())
.ForMember(c => c.Posts, option => option.Ignore());
Run Code Online (Sandbox Code Playgroud)
地图
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
和错误
缺少类型映射配置或不支持的映射.映射类型:CategoriesViewModel - > Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D NewsCMS.Areas.Admin.Models.CategoriesViewModel - > System.Data.Entity.DynamicProxies.Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D
目的地路径:Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D
来源价值:NewsCMS.Areas.Admin.Models.CategoriesViewModel
我错过了什么?我试图找到,但我看不出问题.
UPDATE
我在Global.asax中的application_start中指定了
protected void Application_Start()
{
InitializeAutoMapper.Initialize();
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
Mar*_*sen 55
你在哪里指定了映射代码(CreateMap)?参考:我在哪里配置AutoMapper?
如果您使用静态Mapper方法,则每个AppDomain只应进行一次配置.这意味着放置配置代码的最佳位置是应用程序启动,例如ASP.NET应用程序的Global.asax文件.
如果在调用Map方法之前未注册配置,您将收到 Missing type map configuration or unsupported mapping.
Pie*_*rry 21
在类AutoMapper配置文件中,您需要为实体和视图模型创建一个映射.
这通常是在 AutoMapper/DomainToViewModelMappingProfile
在Configure(),添加一行像
Mapper.CreateMap<YourEntityViewModel, YourEntity>();
Run Code Online (Sandbox Code Playgroud)
在ViewModelToDomainMappingProfile,添加:
Mapper.CreateMap<YourEntity, YourEntityViewModel>();
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 18
注意Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D异常中的类?这是一个实体框架代理.我建议你处理你的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();
}
Run Code Online (Sandbox Code Playgroud)
如果实体检索是在数据访问层内执行的(当然这是正确的方法),请确保在从DAL返回实例之前处置EF上下文.
我找到了解决方案,谢谢大家的回复。
category = (Categoies)AutoMapper.Mapper.Map(viewModel, category, typeof(CategoriesViewModel), typeof(Categoies));
Run Code Online (Sandbox Code Playgroud)
但是,我已经不知道原因了。我无法完全理解。
我这样做是为了删除错误:
Mapper.CreateMap<FacebookUser, ProspectModel>();
prospect = Mapper.Map(prospectFromDb, prospect);
Run Code Online (Sandbox Code Playgroud)
检查您的 Global.asax.cs 文件并确保此行在那里
AutoMapperConfig.Configure();
Run Code Online (Sandbox Code Playgroud)
小智 6
我在 .Net Core 中遇到了同样的问题。因为我的基本 dto 类(我在启动时将其作为 automapper 程序集的类型)在不同的项目中。Automapper 尝试在基类项目中搜索配置文件。但是我的 dto 在不同的项目中。我移动了我的基类。问题解决了。这可能对某些人有所帮助。
就我而言,我已经创建了地图,但缺少 ReverseMap 函数。添加它就消除了错误。
private static void RegisterServices(ContainerBuilder bldr)
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new CampMappingProfile());
});
...
}
public CampMappingProfile()
{
CreateMap<Talk, TalkModel>().ReverseMap();
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
144362 次 |
| 最近记录: |