MVC4和AutoMapper 4.2.1:属性无法映射错误

时间:2016-05-18 17:46:06

标签: asp.net-mvc-4 automapper

我在将数据写入数据库时​​遇到问题。我将所有加载到View Model中的数据都很好,但是当我使用AutoMapper将数据从视图模型转储到db模型时我得到了

  

缺少类型映射配置或不支持的映射。   映射类型:   AttendanceTrackingEntry - > AttendanceTracking   eventTracking.Models.ViewModels.AttendanceTrackingEntry - > eventTracking.Models.AttendanceTracking   目的地路径:   AttendanceTracking   来源价值:   eventTracking.Models.ViewModels.AttendanceTrackingEntry   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。   异常详细信息:AutoMapper.AutoMapperMappingException:缺少类型映射配置或不支持的映射。   映射类型:   AttendanceTrackingEntry - > AttendanceTracking   eventTracking.Models.ViewModels.AttendanceTrackingEntry - > eventTracking.Models.AttendanceTracking   目的地路径:   AttendanceTracking   来源价值:   eventTracking.Models.ViewModels.AttendanceTrackingEntry

这就是我所做的:

数据库模型:

public partial class AttendanceTracking
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public AttendanceTracking()
    {
        this.AttendanceItems = new HashSet<AttendanceItem>();
    }

    public long memberAttendanceID { get; set; }
    public int EventID { get; set; }
    public string MemberNo { get; set; }
    public System.DateTime ArrivalTime { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<AttendanceItem> AttendanceItems { get; set; }
    public virtual @event @event { get; set; }
}

查看型号:

public class AttendanceTrackingEntry
{        
    public Int32 EventID { get; set; }
    public String memberNo { get; set; }
    public DateTime ArrivalTime { get; set; }
    public List<AttendanceItems> AttendanceItems { get; set; }
}

App_Start中的MappingConfig.cs:

Mapper.Initialize(config =>
        {
            config.CreateMap<AttendanceTrackingEntry, AttendanceTracking>()                    
                .ForMember(dest => dest.EventID,
                           opt => opt.MapFrom(src => src.EventID))
                .ForMember(dest => dest.MemberNo,
                           opt => opt.MapFrom(src => src.memberNo))
                .ForMember(dest => dest.ArrivalTime,
                           opt => opt.MapFrom(src => src.ArrivalTime))
                .ForMember(dest => dest.AttendanceItems,
                           opt => opt.MapFrom(src => src.AttendanceItems)); 
                .ForMember(dest => dest.@event,
                           opt => opt.Ignore())
                .ForMember(dest => dest.memberAttendanceID,
                          opt => opt.Ignore());                   
        });
        Mapper.Initialize(config =>
        {
            config.CreateMap<AttendanceItems, AttendanceItem>()
                .ForMember(dest => dest.EventItemsID,
                           opt => opt.MapFrom(src => src.EventItemsID))
                .ForMember(dest => dest.answer,
                           opt => opt.MapFrom(src => src.Answer))
                .ForMember(dest => dest.MemberAttendanceID,
                           opt => opt.Ignore())                    
                .ForMember(dest => dest.AttendanceTracking,
                          opt => opt.Ignore())
                .ForMember(dest => dest.eventItem,
                          opt => opt.Ignore());
        });

最后是控制器中的呼叫:

if (ModelState.IsValid)
        {
            AttendanceTracking LogAttendance = new AttendanceTracking();
            //Mapper.AssertConfigurationIsValid();
            LogAttendance = Mapper.Map<AttendanceTrackingEntry, AttendanceTracking> (attendanceTrackingEntry);                

            db.SaveChanges();  

请注意控制器代码会继续使用正确的语法来完成,如果我没有在此片段中显示它。

我通读Automapper missing type map configuration or unsupported mapping?,但没有帮助。

我最终尝试添加Mapper.AssertConfigurationIsValid();

得到了:

  

无法映射eventTracking.Models.AttendanceItem上的以下属性:   AttendanceItems   添加自定义映射表达式,忽略,添加自定义解析程序或修改目标类型eventTracking.Models.AttendanceItem。   语境:   映射到属性AttendanceItems从eventTracking.Models.ViewModels.AttendanceItems到eventTracking.Models.AttendanceItem   映射到属性AtitudeanceItems from System.Collections.Generic.List 1[[eventTracking.Models.ViewModels.AttendanceItems, eventTracking, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.ICollection 1 [[eventTracking.Models.AttendanceItem,eventTracking,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]   从eventTracking.Models.ViewModels.AttendanceTrackingEntry类型映射到eventTracking.Models.AttendanceTracking   抛出了“AutoMapper.AutoMapperConfigurationException”类型的异常。

所以问题似乎是将视图模型列表项写入数据模型列表。

有关如何使其发挥作用的任何建议吗?

1 个答案:

答案 0 :(得分:0)

您正在两次调用Mapper.Initialize()。您应该在单个初始化下嵌套两个映射配置:

Mapper.Initialize(
            config =>
            {
                config.CreateMap<AttendanceTrackingEntry, AttendanceTracking>()
                        .ForMember(dest => dest.EventID, opt => opt.MapFrom(src => src.EventID))
                        .ForMember(dest => dest.MemberNo, opt => opt.MapFrom(src => src.memberNo))
                        .ForMember(dest => dest.ArrivalTime, opt => opt.MapFrom(src => src.ArrivalTime))
                        .ForMember(dest => dest.AttendanceItems, opt => opt.MapFrom(src => src.AttendanceItems))
                        .ForMember(dest => dest.@event, opt => opt.Ignore())
                        .ForMember(dest => dest.memberAttendanceID, opt => opt.Ignore());

                config.CreateMap<AttendanceItems, AttendanceItem>()
                        .ForMember(dest => dest.EventItemsID, opt => opt.MapFrom(src => src.EventItemsID))
                        .ForMember(dest => dest.answer, opt => opt.MapFrom(src => src.Answer))
                        .ForMember(dest => dest.MemberAttendanceID, opt => opt.MapFrom(src => src.MemberAttendanceID))
                        .ForMember(dest => dest.AttendanceTracking, opt => opt.Ignore())
                        .ForMember(dest => dest.eventItem, opt => opt.Ignore());
            });