Last active
January 2, 2022 20:29
-
-
Save margauxflores/ac07a237472aef51589f064317509332 to your computer and use it in GitHub Desktop.
AutoMapper Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically main problem is, when I map
Vessels
from theModels
(as in the one I made) toGetAllVesselsDto
in the AutoMapper profile, when I try to access the endpoint that calls GetAllVessels, it returns an error:But when I use
Vessels
fromEntities
generated by Pomelo Entity Framework scaffolding, it works perfectly.