Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper throws Missing Map when Map exists

Specifications: .NET 4.5.1 - MVC 5.2.2 - EF 6.0 - AutoMapper 3.2.1

I was first having Proxy object error but was able to solve it by doing the following: AutoMapper 3.1.1 and Entity Framework 6.1 Proxy objects

Once I fixed that error, I instantly got the following error message:enter image description here

For some reason, it says the map from Pages to PagesViewModel doesn't exist, even though it does. Here is my code:

In Global.asax.cs:

protected void Application_Start()
{
     ConfigureAutomapper.Configure();
     ....

In AutoMapper.cs (UPDATED)

public static class ConfigureAutomapper
{
    public static void Configure()
    {
        ConfigurePublications();
        ConfigureBlog();
        ConfigureBasic();
        ConfigureLists();
    }

    public static void ConfigurePublications()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<PublicationsMappings>();
        });
    }

    public static void ConfigureBlog()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<BlogMappings>();
        });
    }

    public static void ConfigureBasic()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<BasicMappings>();
        });
    }

    public static void ConfigureLists()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<ListMappings>();
        });
    }
}

...

public class BasicMappings : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "BasicMappings";
            }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<Pages, PagesViewModel>();

            Mapper.CreateMap<PagesViewModel, Pages>();
            ...

I have traced it and it gets in there and creates the map. When it goes to use the conversion, it doesn't work.

For the model and viewModel, all the variable names are the same. I only added data annotations and some sanitize classes set { pageDescription = value.StringSanitizer(); }

The difference comes when declaring linked objects/tables:

Model:

public virtual PageType PageTypes { get; set; }
public virtual SiteMap SiteMap { get; set; }
public virtual ICollection<Rows> Row { get; set; }
public virtual Track Tracks { get; set; }

while my ViewModel:

public PageTypeViewModel PageTypes { get; set; }
public SiteMapViewModel SiteMap { get; set; }
public ICollection<RowsViewModel> Row { get; set; }
public TrackViewModel Tracks { get; set; }

Connects to the viewModels of those models. All of which are mapped in AutoMapper.cs


UPDATE: I have a unit test already in the project:

[TestMethod]
public void AutoMapper_Basic_Configuration_IsValid()
{
      //Arrange

      //Act
      ConfigureAutomapper.ConfigureBasic();

      //Assert
      Mapper.AssertConfigurationIsValid();
 }

It passed all the tests with no mapping errors.

Can anyone give me some insight on this issue? Please let me know if you need more information. Thank you!

like image 348
Termato Avatar asked Sep 26 '14 15:09

Termato


2 Answers

Instead of Mapper.CreateMap<Pages, PagesViewModel>();, use CreateMap<Pages, PagesViewModel>();.

protected override void Configure()
    {
        CreateMap<Pages, PagesViewModel>();

        CreateMap<PagesViewModel, Pages>();
     ...

Inside a profile, you need to refer to the instance of Mapper which is this.CreateMap or simply CreateMap.

Update Updating my answer so that future readers don't have to wade thru the comments to figure out the issue.

The problem was with multiple calls to Mapper.Initialize. Each new call would clear out previous initialization. The fix was to have a single initialization call and add all profiles in there.

like image 69
Mrchief Avatar answered Sep 19 '22 02:09

Mrchief


There's probably something configured incorrectly.

You should add the following unit test

        [Test]
        public void AreMappingsValid()
        {
            ConfigureAutomapper.Configure();
            AutoMapper.Mapper.AssertConfigurationIsValid();
        }
like image 30
dove Avatar answered Sep 20 '22 02:09

dove