找不到映射

时间:2015-10-22 12:44:50

标签: c# asp.net asp.net-mvc automapper

我一直在努力使用automapper和嵌套对象。我收到以下错误:

Missing type map configuration or unsupported mapping.
AdminSchema -> Schema

但这对我来说似乎不对,因为我确实配置了以下映射:

Mapper.CreateMap<Schema, AdminSchema>().ReverseMap();

应该映射以下模型:

public class AdminSchema
{
    [Required]
    [Display(Name = "Opeenvolgende weken")]
    public int ConsecutiveWeeks { get; set; }

    public List<AdminWorkDay> WorkDays { get; set; }

    public bool Delete { get; set; }
}

public class AdminWorkDay
{
    public int DayOfWeekNumber { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public TimeSpan BreakTime { get; set; }
    public bool Delete { get; set; }
}

public class Schema
{
    public int Id { get; set; }

    public int ConsecutiveWeeks { get; set; }
    public List<WorkDay> WorkDays { get; set; } 
}

public class WorkDay
{
    public int Id { get; set; }

    public int DayOfWeekNumber { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get;set; }
    public TimeSpan BreakTime { get; set; }
}

对于字符串到日期时间的映射,我有以下代码:

Mapper.CreateMap<string, DateTime>().ConvertUsing<StringToDateTimeConverter>();

public class StringToDateTimeConverter : ITypeConverter<string, DateTime>
{
    public DateTime Convert(ResolutionContext context)
    {
        var sourceDt = context.SourceValue;
        DateTime targetDt;

        if (sourceDt == null)
        {
            return default(DateTime);
        }
        return DateTime.TryParse(sourceDt.ToString(), out targetDt) ? targetDt : default(DateTime);
    }
}

以下是映射的实现:

var workPeriod = new WorkPeriod {
            Schemas = Mapper.Map<List<AdminSchema>, List<Schema>>(workPeriodVm.Schemas)
        };

如果有人可能知道为什么找不到我的地图,请告诉我。非常感谢。其他映射确实有效,因此一般来说,自动播放器的配置是正确的。

1 个答案:

答案 0 :(得分:0)

显然,TSource和TDestination中WorkDays的类型不相等,而你必须在AutoMapper Nuget Package中设置相同的TSource和TDestination类型。 例如,将AdminWorkDay属性的TSource和TDestination类型更改为WorkDays,您会看到它正常工作。