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

问题描述 投票:0回答:1

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

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

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

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

在模型层中,我创建了一个Automapper概要文件类。

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)
c# asp.net-core automapper
1个回答
0
投票

您的映射配置文件在“模型”程序集中定义,Startup类在“ API”程序集中。

您有:

services.AddAutoMapper(typeof(Startup));

Automapper将在定义了Startup类型的装配体中搜索轮廓。那不是您要的东西。修改对AddAutoMapper()的调用,并从“模型”程序集传递类型。

© www.soinside.com 2019 - 2024. All rights reserved.