AutoMapper throws Missing Map when Map exists

↘锁芯ラ 提交于 2019-12-10 19:07:58

问题


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:

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!


回答1:


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.




回答2:


There's probably something configured incorrectly.

You should add the following unit test

        [Test]
        public void AreMappingsValid()
        {
            ConfigureAutomapper.Configure();
            AutoMapper.Mapper.AssertConfigurationIsValid();
        }


来源:https://stackoverflow.com/questions/26062743/automapper-throws-missing-map-when-map-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!