C# aps.net类型为';AutoMapper.AutoMapperMappingException';发生在AutoMapper.dll中,但未在用户代码中处理
这件事让我很难受。这是一个可以创建和更新输入详细信息的系统。create部分工作正常,但是当我尝试运行更新时,它抛出AutoMapper错误。这是映射程序代码C# aps.net类型为';AutoMapper.AutoMapperMappingException';发生在AutoMapper.dll中,但未在用户代码中处理,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,这件事让我很难受。这是一个可以创建和更新输入详细信息的系统。create部分工作正常,但是当我尝试运行更新时,它抛出AutoMapper错误。这是映射程序代码 config.CreateMap<DiagnosticCenter, DiagnosticCenterViewModel>() .ForMember(dest => dest.StaffId, opt => opt.MapFrom(src => src.User
config.CreateMap<DiagnosticCenter, DiagnosticCenterViewModel>()
.ForMember(dest => dest.StaffId, opt => opt.MapFrom(src => src.UserId));
config.CreateMap<DiagnosticCenterInputModel, DiagnosticCenter>()
.ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.StaffId))
.ForMember(dest => dest.TestNams, opt => opt.Ignore());
config.CreateMap<DiagnosticCenterInputModel, DiagnosticCenter>()
.ConvertUsing<DiagnosticCenterInputModelToDiagCenterCoverter>();
**this is the inputmodel**
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using KYDS.Infrastructure.DataAccess.Model;
namespace KYDS.ApplicationServices.Models
{
public class DiagnosticCenterInputModel
{
public int Id { get; set; }
public int StaffId { get; set; }
public string TestName { get; set; }
[Required]
public string TestCenterName { get; set; }
[Required]
public string AppointmentDateTime { get; set; }
public string AppointmentTime { get; set; }
public List<string> TestNames { get; set; }
//public int[] SelectedNames { get; set; }
//public DiagnosticCenterInputModel()
//{
// TestNames = new List<TestName>();
//}
}
}
**the model**
using System;
using System.Collections.Generic;
namespace KYDS.Infrastructure.DataAccess.Model
{
public class DiagnosticCenter : BaseEntity
{
public int UserId { get; set; }
public virtual User User { get; set; }
public string TestName { get; set; }
public string TestCenterName { get; set; }
//public ICollection<TestName> TestNames { get; set; }
public string AppointmentDateTime { get; set; }
public string AppointmentTime { get; set; }
public virtual List<string> TestNams { get; set; }
}
}
config.CreateMap()
.ForMember(dest=>dest.StaffId,opt=>opt.MapFrom(src=>src.UserId));
config.CreateMap()
.ForMember(dest=>dest.UserId,opt=>opt.MapFrom(src=>src.StaffId))
.ForMember(dest=>dest.TestNams,opt=>opt.Ignore());
config.CreateMap()
.ConvertUsing();
**这是输入模型**
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Web.Mvc;
使用KYDS.Infrastructure.DataAccess.Model;
命名空间KYDS.ApplicationServices.Models
{
公共类DiagnosticCenterInputModel
{
公共int Id{get;set;}
public int StaffId{get;set;}
公共字符串TestName{get;set;}
[必需]
公共字符串TestCenterName{get;set;}
[必需]
公共字符串指定日期时间{get;set;}
公共字符串指定时间{get;set;}
公共列表TestNames{get;set;}
//public int[]SelectedNames{get;set;}
//公共诊断中心输入模型()
//{
//TestNames=新列表();
//}
}
}
**模型**
使用制度;
使用System.Collections.Generic;
命名空间KYDS.Infrastructure.DataAccess.Model
{
公共类诊断中心:BaseEntity
{
public int UserId{get;set;}
公共虚拟用户用户{get;set;}
公共字符串TestName{get;set;}
公共字符串TestCenterName{get;set;}
//公共ICollection测试名称{get;set;}
公共字符串指定日期时间{get;set;}
公共字符串指定时间{get;set;}
公共虚拟列表TestNams{get;set;}
}
}
这是appservice(错误来源)
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用KYDS.ApplicationServices.abstraction;
使用KYDS.ApplicationServices.Models;
使用KYDS.Infrastructure.DataAccess.Model;
使用KYDS.Infrastructure.DataAccess.Repository.Abstractions;
使用KYDS.Infrastructure.DataAccess.UnitOfWork;
使用KYDS.Infrastructure.Logging;
命名空间KYDS.ApplicationServices
{
类DiagnosticCenterAppService:IDiagnosticCenterAppService
{
专用只读IRepository存储库;
私人只读i工作单元(unitof工作单元);;
专用只读ILog_记录器;
公共诊断中心AppService(IRepository存储库、IUnitOfWork unitOfWork)
{
_存储库=存储库;
_unitOfWork=unitOfWork;
_logger=LogProvider.For();
}
公共诊断中心InputModel GetDiagnosticCenter(int id)
{
var diagnosticCenter=\u repository.GetById(id);
返回AutoMapper.Mapper.Map(诊断中心);
}
公共操作结果更新(诊断中心输入模型)
{
OperationResult=新的OperationResult();
尝试
{
var diagnosticCenter=\u repository.GetById(model.Id);
if(诊断中心!=null)
{
AutoMapper.Mapper.Map(模型、诊断中心);
_存储库更新(诊断中心);
_unitOfWork.Commit();
结果:成功=正确;
}
其他的
{
_logger.Info(()=>$“找不到ID为“{model.ID}”的任何医疗信息记录进行更新。”);
结果:成功=错误;
result.AddErrorMessage(“无法找到要更新的记录”);
}
}
//在调用常规catch块之前,捕获可能发生的某些特定错误
捕获(参数异常)
{
_logger.Error($”尝试更新ID为{model.ID}的记录时出错。\n异常:\t{ex.Message}”);
结果:成功=错误;
result.AddErrorMessage(“尝试更新记录时出错”);
}
捕获(例外情况除外)
{
_logger.Error($”尝试更新ID为{model.ID}的记录时出错。\n异常:\t{ex.Message}”);
结果:成功=错误;
result.AddErrorMessage(“尝试更新记录时出错”);
}
返回结果;
}
}
}
如果您有任何帮助,我们将不胜感激。请检查您创建映射的位置是否忽略了任何不兼容的属性,或者如果它们具有内部集合,则它们也已正确映射
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KYDS.ApplicationServices.Abstractions;
using KYDS.ApplicationServices.Models;
using KYDS.Infrastructure.DataAccess.Model;
using KYDS.Infrastructure.DataAccess.Repository.Abstractions;
using KYDS.Infrastructure.DataAccess.UnitOfWork;
using KYDS.Infrastructure.Logging;
namespace KYDS.ApplicationServices
{
class DiagnosticCenterAppService : IDiagnosticCenterAppService
{
private readonly IRepository<DiagnosticCenter> _repository;
private readonly IUnitOfWork _unitOfWork;
private readonly ILog _logger;
public DiagnosticCenterAppService(IRepository<DiagnosticCenter> repository, IUnitOfWork unitOfWork)
{
_repository = repository;
_unitOfWork = unitOfWork;
_logger = LogProvider.For<DiagnosticCenterAppService>();
}
public DiagnosticCenterInputModel GetDiagnosticCenter(int id)
{
var diagnosticCenter = _repository.GetById(id);
return AutoMapper.Mapper.Map<DiagnosticCenterInputModel>(diagnosticCenter);
}
public OperationResult Update(DiagnosticCenterInputModel model)
{
OperationResult result = new OperationResult();
try
{
var diagnosticCenter = _repository.GetById(model.Id);
if (diagnosticCenter != null)
{
AutoMapper.Mapper.Map(model, diagnosticCenter);
_repository.Update(diagnosticCenter);
_unitOfWork.Commit();
result.Success = true;
}
else
{
_logger.Info(() => $"Could not find any Medical Information record with ID '{model.Id}' to update.");
result.Success = false;
result.AddErrorMessage("Could not find record to update.");
}
}
//catch some specific errors that might occur, before calling the general catch block
catch (ArgumentException ex)
{
_logger.Error($"Error occurred while trying to update record with ID {model.Id}. \n Exception: \t {ex.Message}");
result.Success = false;
result.AddErrorMessage("Error occurred while trying to update record");
}
catch (Exception ex)
{
_logger.Error($"Error occurred while trying to update record with ID {model.Id}. \n Exception: \t {ex.Message}");
result.Success = false;
result.AddErrorMessage("Error occurred while trying to update record");
}
return result;
}
}
}