库中的 AutoMapper 配置文件 class - AutoMapperMappingException:缺少类型映射配置或不支持的映射错误

AutoMapper profile class in library - AutoMapperMappingException: Missing type map configuration or unsupported mapping error

我正在尝试在多层 .Net Core Web 中使用 AutoMapper API。这是图层

  1. API - 所有控制器(Web API)
  2. 业务——业务逻辑(库)
  3. 模型 - DTO 和 Automapper 配置文件(库)
  4. 数据 - EF 层(库)

在 Web API 中,我在启动时注入了所需的接口和具体 class 以及初始化 AutoMapper,还引用了 Models 项目

  services.AddAutoMapper(typeof(Startup));
  services.AddDbContextPool<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
  services.AddScoped<IStudent, Student.Business.Student>();

在模型层,我创建了一个 Automapper 配置文件 class。

namespace Student.Models.Mapper
{
    public class StudentProfile : Profile
    {
        public StudentProfile()
        {
            CreateMap<Student, Entities.Student>().ReverseMap();
            CreateMap<StudentAddress, Entities.StudentAddress>().ReverseMap();
        }
    }
}

在业务层

namespace Student.Business
{
    public class Student : BaseClass, IStudent
    {
        private readonly ApplicationContext _context;
        private readonly IMapper _mapper;


        public Student(ApplicationContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        public async Task<Response<Models.Student>> CreateStudentAsync(Models.Student student)
        {
            var studentEntity = _mapper.Map<Entities.Student>(student);
            _context.Students.Add(studentEntity);
            await _context.SaveChangesAsync();
            var response = new Response<Models.Student>
            {
                Content = student
            };
            return response;
        }
    }
}

我收到以下错误

AutoMapperMappingException: Missing type map configuration or unsupported mapping. Mapping types: Student -> Student Student.Models.Student -> Student.Entities.Student

lambda_method(Closure , Student , Student , ResolutionContext )
lambda_method(Closure , object , object , ResolutionContext )
AutoMapper.Mapper.Map<TDestination>(object source) in Mapper.cs
Student.Business.Student.CreateStudentAsync(Student student) in Student.cs

var studentEntity = _mapper.Map<Entities.Student>(student);

Student.Api.Controllers.StudentsController.CreateStudentAsync(Student student) in StudentsController.cs

return await _student.CreateStudentAsync(student);

lambda_method(Closure , object )
Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable+Awaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

您收到此错误是因为 AutoMapper 找不到您的映射配置文件。

您的映射配置文件在 "Models" 程序集中定义,Startup class 在 "API" 程序集中。

你有:

services.AddAutoMapper(typeof(Startup));

Automapper 将在定义了类型 Startup 的程序集中搜索配置文件。那不是你要找的。修改对 AddAutoMapper() 的调用并从 "Models" 程序集传递一个类型。