I've had issues with AutoMapper 3 mapping related entities from entity framework to a view model some of the time in MVC 5. I'm using EF 6.1.1 to get the data, and a thin and light repository layer to centralize queries. That issue only seems to occur with mapping properties from entities under a root entity, but the problem is intermittent. I've seen the behavior start working again after rebuilding the solution and the behavior break after rebuilding the solution without changing the code where the issue occurs.
I've tested this in the debugger and SQL profiler, and even when the data isn't showing up on the front end, I can see in the SQL profiler that Entity Framework is loading the related tables.
Example mapping with the issue
This code executes in the MVC controller after the data has been queried from the repository layer. The view model will then be passed off to the view for rendering.
Mapper.CreateMap<Event, EventViewModel>()
.ForMember(e => e.EventTitleId, opt => opt.MapFrom(x => x.EventTitleType.EventTitleId));
EventViewModel model = Mapper.Map<Event, EventViewModel>(eventItem);
//Fix Up I had to implement to ensure the data gets into the property
model.EventTitleId = eventItem.EventTitleType.EventTitleId;
Another example mapping with the issue
Same basic execution path under a different controller action, same problem.
Mapper.CreateMap<Event, EventViewModel>()
.ForMember(l => l.EventTitle, opt => opt.MapFrom(x => x.EventTitleType.EventTitle.Title))
.ForMember(l => l.EventType, opt => opt.MapFrom(x => x.EventTitleType.LkEventType.Title))
.ForMember(l => l.LocationName, opt => opt.MapFrom(x => x.LkLocation.LocationName));
IEnumerable<EventViewModel> eventList = Mapper.Map<IEnumerable<Event>, IEnumerable<EventViewModel>>(events);
Again, sometimes the three properties get data, and sometimes they don't.
Data Model
This is what the Data Model looks like.
Repository
The repository layers wraps up the entity framework calls, but it does not abstract away the entity framework entities and the issues and benefits it has. All repositories in the project inherit IRepository, and I'm using this layer to centralize query and business logic.
using System.Collections.Generic;
namespace Tools.Repository
{
/// <summary>
/// Generic Repository Interface
/// </summary>
/// <typeparam name="TClass">Class that the Repository is implemented for.</typeparam>
public interface IRepository<TClass>
where TClass : class
{
void SaveChanges();
TClass Find(params object[] keyValues);
IEnumerable<TClass> GetAll();
TClass Add(TClass itemToAdd);
TClass Update(TClass itemToUpdate);
TClass Remove(TClass itemToDelete);
}
}
Summary
I don't believe this is an Entity Framework lazy loading issue, and after forcing EF to eager load the data, the problem persists. The entities have the values in the debugger before mapping, but only sometimes will the value get mapped to the view model. The most frustrating thing is that the issue does not seem to be caused by any changes I've made to the view model.
What can cause such inconsistent behavior? I've had this problem with both AutoMapper 3.2.1 and 3.1.1.