Asp.net is a server-side, open-source, and free web framework that is mostly used for building dynamic websites and applications. It is a technology that helps you enable computer code to run by an internet server. The applications and services programmers create can easily run on different operating systems like Windows, Linux, macOS, and Docker. Asp.net is created by Microsoft and it is capable of creating various projects that programmers are also working on. It is a low-cost and high-speed programming language that is quite easier to learn as it doesn’t need a long procedure or setup. When working with asp.net you may encounter “missing type map configuration or unsupported mapping problem”.
Sometimes errors can make you stuck in between the programming where you don’t understand what to do. We are here to help you out and provide you with tips and solutions to fix the error warning that keeps popping up. But first, let’s check out how the error occurs
How do you get the error?
When you try to run a map project, you get the following error warning
Missing type map configuration or unsupported mapping. Mapping types: CategoriesViewModel -> Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D NewsCMS.Areas.Admin.Models.CategoriesViewModel -> System.Data.Entity.DynamicProxies.Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D
Destination path: Categoies_7314E98C41152985A4218174DDDF658046BC82AB0ED9E1F0440514D79052F84D
Source value: NewsCMS.Areas.Admin.Models.CategoriesViewModel
It is what you get when you use the below program code
Entity model:
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; }
}
View model:
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());
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);
}
}
Global.asax:
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());
}
}
This is the project code that results in the error warning.
How To Fix the Error “missing type map configuration or unsupported mapping problem”
The cause of the error is the unregistered configuration before the Map method is called. Check out the options you can try to remove the error
Option 1 – Map creation for entity and ViewModel
In order to get rid of the error warning, you need to create a map for the entity and ViewModel in the AutoMapper class. To do that, you need to add a code in your program.
Domain model to ViewModel mapping
Here, you need to add a code in ViewModelToDomainMappingProfile. Check out the code
Mapper.CreateMap<YourEntity, YourEntityViewModel>();
ViewModel to Domain model mapping
The line should be added in the configure() that is usually in the AutoMapper/DomainToViewModelMappingProfile
Mapper.CreateMap<YourEntityViewModel, YourEntity>();
It can help you resolve the error.
Option 2 – Map configuration code placement
It is important to place the configuration code in the right place to make the code work without an error warning. In the case of utilizing the static Mapper method, once per AppDomain is the configuration that should be set. So, the best way to place the configuration code is in the startup of the application like the Global.asax file. Map configuration code should be set in order to remove the error warning.
Conclusion
And that’s how you can successfully make the error “missing type map configuration or unsupported mapping problem” go away. These simple options are easy to implement as well as effective. I wish you luck!