你好,
我目前正在构建ASP.NET后端。 对于实体和模型对象的映射,我使用的是AutoMapper(4.2.1版)。 当我向邮递员发送创建任务的邮寄请求时,总是会收到一个异常:
缺少类型映射配置或不支持的映射。
映射类型:NewTask->任务TaskManager.Web.Api.Models.NewTask-> TaskManager.Data.Entities.Task
目标路径:任务
源值:TaskManager.Web.Api.Models.NewTask
我尝试了几种方法,但从未得到成功的结果。
这是我的AutoMapperAdapter.cs:
我总是在此文件中收到错误消息
namespace TaskManager.Common.TypeMapping {
public class AutoMapperAdapter : IAutoMapper {
public T Map<T>(object objectToMap) {
try {
return Mapper.Map<T>(objectToMap);
} catch (Exception ex) {
throw ex;
}
}
}
}
以及AddTaskMaintenance处理器:
namespace TaskManager.Web.Api.MaintenanceProcessing {
public class AddTaskMaintenanceProcessor : IAddTaskMaintenanceProcessor {
private readonly IAutoMapper _autoMapper;
private readonly IAddTaskQueryProcessor _queryProcessor;
public AddTaskMaintenanceProcessor(IAddTaskQueryProcessor queryProcessor, IAutoMapper autoMapper) {
_queryProcessor = queryProcessor;
_autoMapper = autoMapper;
}
public Models.Task AddTask(NewTask newTask) {
var taskEntity = _autoMapper.Map<Data.Entities.Task>(newTask);
_queryProcessor.AddTask(taskEntity);
var task = _autoMapper.Map<Models.Task>(taskEntity);
return task;
}
}
}
模型对象到实体和反向对象的映射如下:
namespace TaskManager.Web.Api.AutoMappingConfiguration {
public class NewTaskToTaskEntityAutoMapperTypeConfigurator : IAutoMapperTypeConfigurator {
public void Configure() {
Mapper.Initialize(conf => {
conf.CreateMap<NewTask, TaskManager.Data.Entities.Task>()
.ForMember(opt => opt.Version, x => x.Ignore())
.ForMember(opt => opt.CreatedBy, x => x.Ignore())
.ForMember(opt => opt.TaskId, x => x.Ignore())
.ForMember(opt => opt.CreatedDate, x => x.Ignore())
.ForMember(opt => opt.CompletedDate, x => x.Ignore())
.ForMember(opt => opt.Status, x => x.Ignore())
.ForMember(opt => opt.Users, x => x.Ignore());
});
}
}
}
另一个文件:
namespace TaskManager.Web.Api.AutoMappingConfiguration {
public class TaskEntitiyToTaskAutoMapperTypeConfigurator : IAutoMapperTypeConfigurator {
public void Configure() {
Mapper.Initialize(conf => {
conf.CreateMap<Task, Models.Task>()
.ForMember(opt => opt.Links, x => x.Ignore())
.ForMember(opt => opt.Status, x => x.Ignore())
.ForMember(opt => opt.Assignees, x => x.ResolveUsing<TaskAssigneesResolver>());
});
}
}
}
我的问题:
我的代码可能是什么问题?
我必须升级AutoMapper的版本吗?