Martin Mazza Dawson Martin Mazza Dawson - 15 days ago 8
C# Question

ASP.net AutoMapper Missing type map configuration or unsupported mapping

Domain model:

public class Test
{
public int Id { get; set; }
}


View model:

public class TestViewModel
{
public int Id { get; set; }
}


Global.asax:

AutoMapper.Initialize();


AutoMapper:

public static class AutoMapper
{
public static void Initialize()
{
CreateModelsToViewModels();
CreateViewModelsToModels();
}

private static void CreateModelsToViewModels()
{
Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>().ReverseMap());

Mapper.AssertConfigurationIsValid();
}

private static void CreateViewModelsToModels()
{

}
}


Configuration is valid.

public ActionResult Index(string category)
{
Test t = _Context.test.First(x => x.Id == 1);
var test = Mapper.Map<Test, TestViewModel>(t); //error here
}


Only data is one row with an Id of 1. Even this simple test keeps throwing me the error
Missing type map configuration or unsupported mapping
.
What could be the problem?

Edit: Changed
Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>().ReverseMap());
to
Mapper.Initialize(x => x.CreateMap<Test, TestViewModel>());
and still same error

Answer

It turns out I had forgotten to update the database (Using database migration) after creating these test classes to map.

For anyone else having this problem, make sure you use update-database in package manager console if your using database migrations, otherwise you get this confusing error:

AutoMapper Missing type map configuration or unsupported mapping.

Comments