AutoMapper throws away missing map when map exists
Specifications: .NET 4.5.1 - MVC 5.2.2 - EF 6.0 - AutoMapper 3.2.1
At first I had the Proxy object error, but I was able to solve it by following these steps: AutoMapper 3.1.1 and Entity Framework 6.1 Proxy Objects
As soon as I fixed this error, I immediately received the following error message:
For some reason, it says that the map from Pages to PagesViewModel doesn't exist even if 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 traced it and it gets there and creates a map. When it switches to using a transform, it doesn't work.
For model and viewModel, all variable names are the same. I only added data annotations and some sanitation classesset { pageDescription = value.StringSanitizer(); }
The difference occurs when declaring related 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; }
and 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 these models. They all show up 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 tests without display errors.
Can anyone give me some insight into this issue? Please let me know if you need more information. Thank!
source to share
Mapper.CreateMap<Pages, PagesViewModel>();
Use instead CreateMap<Pages, PagesViewModel>();
.
protected override void Configure()
{
CreateMap<Pages, PagesViewModel>();
CreateMap<PagesViewModel, Pages>();
...
Inside the profile, you need to refer to an instance Mapper
that is equal this.CreateMap
or just CreateMap
.
Update Updating my answer so that future readers don't have to wade through the comments to figure out the problem.
The problem was multiple calls Mapper.Initialize
. Each new call clears the previous initialization. The fix should consist of one initialization call and adding all profiles.
source to share