c# - AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping in . 网络核心

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

我在 .NET CORE 应用程序中使用 AutoMapper。我在 ConfigureServices.cs 方法文件中的 Startup.cs 中配置了 AutoMapper,如下所示:

public void ConfigureServices(IServiceCollection services)
        {

             /* Some Code */
            
            /* AutoMapper Configuration */
            services.AddAutoMapper(typeof(AutoMapperProfileConfiguration).GetType().Assembly);

            services.AddMvcCore();

             /* Some Code */
        }

AutoMapperProfileConfiguration 如下:

public class AutoMapperProfileConfiguration : Profile
    {
        public AutoMapperProfileConfiguration()
        {
            CreateMap<LoginRequestDto, Users>();
        }
    }

然后我在下面的类中使用了这个映射器:

public LoginResponseDto CreateAnAccount(LoginRequestDto loginRequestDto)
        {
            var userInfo = _mapper.Map<Users>(loginRequestDto);
            userInfo.Id = Guid.NewGuid();
            _context.InsertItem<Users>(userInfo);
            var insertedUserDetails = _context.GetItemById<Users>(userInfo.Id);
            insertedUserDetails.Wait();
            return _mapper.Map<LoginResponseDto>(insertedUserDetails.Result);
        }

但是当我尝试将 loginRequestDto 中存在的数据映射到 Users 类时,它给了我以下异常:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
Object -> Users
System.Object -> NeighborhoodHelpers.UserMicroservice.Entities.Models.Users
   at lambda_method(Closure , Object , Users , ResolutionContext )
   at NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.DataAccessProvider\UserDataAccess\UserDataAccess.cs:line 29
   at NeighborhoodHelpers.UserMicroservice.Services.UserServices.UserService.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.Services\UserServices\UserService.cs:line 22
   at NeighborhoodHelpers.UserMicroservice.API.Controllers.LoginController.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.API\Controllers\LoginController.cs:line 34
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

我正在使用 .NET Core 3.1。请帮我解决一下这个。我无法使用 AutoMapper 功能。

最佳答案

像下面这样更改您的 Startup.cs:

services.AddAutoMapper(typeof(AutoMapperProfileConfiguration));

关于c# - AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping in . 网络核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65467481/

相关文章:

c# - Asp.net mvc 基于角色的身份验证

c# - 伪造模拟数据 2 个不同属性的相同值

c# - 指定的值包含无效的 HTTP header 字符

c# - 检查哪个visual studio版本是使用C#代码运行的项目

c# - ASP.NET Gridview ButtonField onclick 触发包含行的 onclick 事件

asp.net-mvc - ASP.NET MVC 3 输入名称和 id 以小写形式

c# - 从 App_Data 中的文件获取 XML 数据并将其转换为 IQueryable<MyModel> 的最有效方法?

asp.net-mvc - 在没有新的 IServiceProvider 实例的情况下指定自定义 IModelBinderProvider?

javascript - 使用 javascript 调用 ViewComponent

c# - InitializeComponent() 有什么作用,它在 WPF 中是如何工作的?