Skip to content

Instantly share code, notes, and snippets.

@margauxflores
Last active January 2, 2022 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save margauxflores/ac07a237472aef51589f064317509332 to your computer and use it in GitHub Desktop.
Save margauxflores/ac07a237472aef51589f064317509332 to your computer and use it in GitHub Desktop.
AutoMapper Problem
// Vessel from Models does not work
using Citadel.Models;
// Vessel from Entities works
using Vessel = Citadel.Entities.Vessel;
namespace Citadel
{
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Vessel, GetAllVesselsDto>();
}
}
}
namespace Citadel.Services.VesselDataService
{
public class VesselDataService : IVesselDataService
{
private readonly IMapper _mapper;
private readonly DataContext _context;
public VesselDataService(IMapper mapper, DataContext context)
{
_context = context;
_mapper = mapper;
}
public async Task<ServiceResponse<List<GetAllVesselsDto>>> GetAllVessels()
{
var serviceResponse = new ServiceResponse<List<GetAllVesselsDto>>();
var vessels = await _context.Vessels.ToListAsync();
serviceResponse.Data = vessels.Select(c => _mapper.Map<GetAllVesselsDto>(c)).ToList();
return serviceResponse;
}
}
}
// Generated by Pomelo Entity Framework Scaffolding from Database
namespace Citadel.Entities
{
public partial class Vessel
{
public Vessel()
{
Cruises = new HashSet<Cruise>();
Responses = new HashSet<Response>();
}
public int Id { get; set; }
public string Code { get; set; }
public string NameEn { get; set; }
public string NameJp { get; set; }
public virtual ICollection<Cruise> Cruises { get; set; }
public virtual ICollection<Response> Responses { get; set; }
}
}
namespace Citadel.Models
{
public class Vessel
{
public int Id { get; set; }
public string Code { get; set; }
public string NameEn { get; set; }
public string NameJp { get; set; }
}
}
@margauxflores
Copy link
Author

Basically main problem is, when I map Vessels from the Models (as in the one I made) to GetAllVesselsDto in the AutoMapper profile, when I try to access the endpoint that calls GetAllVessels, it returns an error:

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

Mapping types:
Object -> GetAllVesselsDto
System.Object -> Citadel.Dtos.VesselData.GetAllVesselsDto
   at lambda_method59(Closure , Object , GetAllVesselsDto , ResolutionContext )
   at Citadel.Services.VesselDataService.VesselDataService.<GetAllVessels>b__3_0(Vessel c) in /Users/margaux/RiderProjects/Citadel/Citadel/Services/VesselDataService/VesselDataService.cs:line 28

But when I use Vessels from Entities generated by Pomelo Entity Framework scaffolding, it works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment