Relationship many to many

38 views
Skip to first unread message

jose pozo

unread,
Jan 17, 2022, 11:11:57 PMJan 17
to AutoMapper-users
How can use path method in c#.

I have obtains this is error:

AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
Producto -> ProductoPatchDTO
SolutionFarma.Models.Producto -> SolutionFarma.DTOs.ProductoPatchDTO

Type Map configuration:
Producto -> ProductoPatchDTO
SolutionFarma.Models.Producto -> SolutionFarma.DTOs.ProductoPatchDTO

Destination Member:
Farmacias

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

Mapping types:
ProductoFarmacia -> ProductoFarmaciaPatchDTO
SolutionFarma.Models.ProductoFarmacia -> SolutionFarma.DTOs.ProductoFarmaciaPatchDTO
   at lambda_method94(Closure , ProductoFarmacia , ProductoFarmaciaPatchDTO , ResolutionContext )
   at lambda_method93(Closure , Object , ProductoPatchDTO , ResolutionContext )
   --- End of inner exception stack trace ---
   at lambda_method93(Closure , Object , ProductoPatchDTO , ResolutionContext )
   at SolutionFarma.Controllers.ProductoController.Patch(Int32 id, JsonPatchDocument`1 patchDocument) in C:\Users\pozoj\source\repos\Farmacia\Farmacia\Controllers\ProductoController.cs:line 115
   at lambda_method5(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   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>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   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 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   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)

HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 273
Content-Type: application/json
Host: localhost:44394
User-Agent: PostmanRuntime/7.28.4
Postman-Token: 884acda7-8fe1-46da-9bb0-a168fe8bfcc2

jose pozo

unread,
Jan 17, 2022, 11:13:26 PMJan 17
to AutoMapper-users
Controller:

[HttpPatch("{id:int}")]
        public async Task<ActionResult> Patch([FromRoute] int id, [FromBody] JsonPatchDocument<ProductoPatchDTO> patchDocument)
        {
            if (patchDocument == null) { return BadRequest(); }

            var productoBD = await this._context.Producto
                .Include(prodFarm => prodFarm.Farmacias)
                .FirstOrDefaultAsync(prod => prod.Id == id);

            if (productoBD == null) { return NotFound(); }

            var productoDTO = this._mapper.Map<ProductoPatchDTO>(productoBD);
            patchDocument.ApplyTo(productoDTO, ModelState);

            var valido = TryValidateModel(productoDTO);
            if (!valido) { return BadRequest(ModelState); }

            this._mapper.Map(productoDTO, productoBD);

            await this._context.SaveChangesAsync();

            return NoContent();
        }

MapperProfile.cs

 public MapperProfile()
        {
            // Controller Producto
            CreateMap<Producto, ProductoDTO>();
            CreateMap<ProductoCreacionDTO, Producto>()
                .ForMember(x => x.Farmacias, opciones => opciones.MapFrom(MapCrearProductoConFarmacia));
            CreateMap<ProductoPatchDTO, Producto>().ReverseMap();
            CreateMap<ProductoFarmaciaPatchDTO, ProductoFarmacia>().ReverseMap();
        }
Reply all
Reply to author
Forward
0 new messages