2

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.

The part of the edmx file with the data model for this question

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.

4

1 回答 1

2

问题是您在使用它们之前创建(配置)映射。现在这是一个巧合的问题,首先创建了哪个映射,因此将占上风。

AutoMapper 文档说:

如果您使用的是静态 Mapper 方法,则每个 AppDomain 只应进行一次配置。这意味着放置配置代码的最佳位置是在应用程序启动中,例如用于 ASP.NET 应用程序的 Global.asax 文件。

Event这意味着您在和之间只有一个映射EventViewModel,这可能应该是您现在拥有的两者的组合。

顺便说一句,您可以通过DynamicMap.

于 2014-06-30T19:56:42.343 回答