gpt4 book ai didi

asp.net-mvc - Automapper 缺少类型映射配置或不支持的映射 - 错误

转载 作者:行者123 更新时间:2023-12-03 05:35:36 29 4
gpt4 key购买 nike

实体模型

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; }
}

创建 map

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

map

[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);
}
}

错误

Missing type map configuration or unsupported mapping.Mapping types:CategoriesViewModel -> Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84DNewsCMS.Areas.Admin.Models.CategoriesViewModel -> System.Data.Entity.DynamicProxies.Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D

Destination path:Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D

Source value:NewsCMS.Areas.Admin.Models.CategoriesViewModel

我错过了什么?我尝试查找,但看不到问题。

更新

我已在 Global.asax 的 application_start 中指定

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

初始化类

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)?引用:Where do I configure AutoMapper?

If you're using the static Mapper method, configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications.

如果在调用 Map 方法之前未注册配置,您将收到缺少类型映射配置或不受支持的映射。

关于asp.net-mvc - Automapper 缺少类型映射配置或不支持的映射 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14677889/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com