我正在尝试在多层.Net Core Web API中使用AutoMapper。这是图层
在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)
您的映射配置文件在“模型”程序集中定义,Startup
类在“ API”程序集中。
您有:
services.AddAutoMapper(typeof(Startup));
Automapper将在定义了Startup
类型的装配体中搜索轮廓。那不是您要的东西。修改对AddAutoMapper()
的调用,并从“模型”程序集传递类型。