aps.net类型' AutoMapper.AutoMapperMappingException'的例外。发生在AutoMapper.dll中但未在用户代码中处理

时间:2017-01-31 16:55:30

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

我对此很努力。这是一个可以创建和更新输入详细信息的系统。 create部分工作正常,但是当我尝试运行更新时,它会抛出AutoMapper错误。这是映射器代码。

     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; }


    }
}

这是appservice(错误来自哪里)

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;
        }
    }
    }

任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:0)

检查您创建映射的位置是否已忽略任何不兼容的属性,或者如果它们具有内部集合,则它们也会正确映射。