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!
There's probably something configured incorrectly.
You should add the following unit test
Instead of
Mapper.CreateMap<Pages, PagesViewModel>();
, useCreateMap<Pages, PagesViewModel>();
.Inside a profile, you need to refer to the instance of
Mapper
which isthis.CreateMap
or simplyCreateMap
.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.