2

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

5
  • Not at my computer, so I can't verify, but my guess is that you are initializing AutoMapper twice (ModelToVm and VmToModel), thus overwriting one of the initializations when you call a second time? Commented Mar 28, 2016 at 21:48
  • @Sam I have edited post, still getting error. Also I have commented out all my other code, so it is only initializing automapper once for them models. Commented Mar 28, 2016 at 21:52
  • ReverseMap() Creates a map both ways. so I deleted my wrong answer Commented Mar 28, 2016 at 21:55
  • 1
    Where in 'Global.asax.cs' are you calling 'Automapper.Initialize()'? Are you 100% certain that the method is being called? Commented Mar 28, 2016 at 21:59
  • @Will At the end of Application_Start() after the other methods. Yes it is being called, I set a breakpoint. Commented Mar 28, 2016 at 22:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.