-1

我正在使用 Auto mapper 并且收到此错误,我在 AddAsync() 方法中收到此错误。请帮我解决这个问题。

Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nDepartmentsViewModel -> Departments\r\nEmployeeAttendanceApp.ViewModels.DepartmentsViewModel -> EmployeeAttendanceApp.Models.StaffManagement.Departments

我的课

public interface IMapperConfig
{
    IMapper CreateMapper();
}

public class MapperConfig : IMapperConfig
{
    public IMapper CreateMapper()
    {
        var config = new MapperConfiguration(cfg =>
       {
           cfg.CreateMap<RegisterStaffs, RegisterStaffViewModel>();
           cfg.CreateMap<AttendanceRecorder, AttendanceRecorderViewModel>();
           cfg.CreateMap<ManageLeaves, ManageLeavesViewModel>();
           cfg.CreateMap<RegisterDevices, RegisterDevicesViewModel>();
           cfg.CreateMap<Departments, DepartmentsViewModel>();

       });

        return config.CreateMapper();
    }
}

      public class DepartmentsViewModel
{
    [Key]
    public int DepartmentId { get; set; }

    [DisplayName("اسم القسم/ الادارة")]
    [Required(ErrorMessage = "الرجاء ادخال اسم القسم")]
    public string DepartmentName { get; set; }

    [DisplayName("اضافة ملاحظات القسم")]
    public string Remarks { get; set; }
    public bool? IsUpdated { get; set; }
    public bool? IsDeleted { get; set; }
    public string CreatedBy { get; set; }
    public string DeletedBy { get; set; }
    public string UpdatedBy { get; set; }
    [DisplayName("عدد الموظفيين")]
    [Required(ErrorMessage =  " الرحاء إدخال عدد موظفيين القسم")]
    public long? StaffNumber { get; set; }

    public DateTime? CreatedDate { get; set; }

    public virtual ICollection<RegisterDevicesViewModel> RegisterDevices { get; set; }
    public virtual ICollection<RegisterStaffViewModel> RegisterStaffs { get; set; }
}

     public partial class Departments
{
    
    [Key]
    public int DepartmentId { get; set; }

    public string DepartmentName { get; set; }

    public string Remarks { get; set; }
    public bool? IsUpdated { get; set; }
    public bool? IsDeleted { get; set; }
    public string CreatedBy { get; set; }
    public string DeletedBy { get; set; }
    public string UpdatedBy { get; set; }
    public long? StaffNumber { get; set; }
    public DateTime? CreatedDate { get; set; }

    public virtual ICollection<RegisterDevices> RegisterDevices { get; set; }
    public virtual ICollection<RegisterStaffs> RegisterStaffs { get; set; }
}

       **I am getting the error in this method** 

      public async Task<bool> AddAsync(DepartmentsViewModel departmentsView)
    {
        try
        {
            var department = mapper.Map<DepartmentsViewModel, Departments>(departmentsView);
            await context.Departments.AddAsync(department);
            await context.SaveChangesAsync();
            return true;
        }
        catch(Exception ex)
        {
            logger.LogError(ex, ex.Message);
            return false;
        }
    }

       //here is startup file where I have registered the services  
        services.AddSingleton<IMapperConfig, MapperConfig>();

        services.AddTransient<IDepartmentManager, DepartmentManager>();
4

1 回答 1

0

我通过在 MapperConfig 类中添加 .ReverseMap() 解决了这个问题

cfg.CreateMap<Departments, DepartmentsViewModel>().ReverseMap();

但我仍然不明白为什么它以前没有它就不能工作,因为我见过各种各样的例子,从来没有使用过 ReverseMap()

于 2020-07-10T14:44:43.243 回答