I have dto object:
public class TourDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public string TypeOfTransport { get; set; }
public List<string> Countries { get; set; }
public string Description { get; set; }
public string MainText { get; set; }
public int MaxNumberOfClients { get; set; }
public int NumberOfClients { get; set; }
public int? Rating { get; set; }
public int Cost { get; set; }
public DateTime DateOfStart { get; set; }
public DateTime DateOfEnd { get; set; }
}
and I have entity in DB:
public class Tour: IBaseEntity
{
public Guid Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public string? MainText { get; set; }
public int? MaxNumberOfClients { get; set; }
public int? NumberOfClients { get; set; }
public int? Rating { get; set; }
public int? Cost { get; set; }
public DateTime? DateOfStart { get; set; }
public DateTime? DateOfEnd { get; set; }
public Guid TypeOfTransportId { get; set; }
public virtual TypeOfTransport? TypeOfTransport { get; set; }
public virtual List<UserTour>? UsersTours { get; set; }
public virtual List<TourCountry>? ToursCountries { get; set; }
}
Also I have Country entities:
public class Country : IBaseEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual List<TourCountry>? ToursCountries { get; set; }
}
and TourCountry entities, that implement communication many-to-many between Tour and Country:
public class TourCountry : IBaseEntity
{
public Guid Id { get; set; }
public Guid TourId { get; set; }
public virtual Tour Tour { get; set; }
public Guid CountryId { get; set; }
public virtual Country Country { get; set; }
}
I can map Tour to TourDto by this profile:
CreateMap<Tour, TourDto>()
.ForMember(dto => dto.TypeOfTransport,
opt => opt.MapFrom(ent => ent.TypeOfTransport.Name))
.ForMember(dto => dto.Countries,
opt => opt.MapFrom(ent => ent.ToursCountries.Select(tc => tc.Country.Name).ToList()));
But I can't to implement this mapping back, from entity to dto.
I try to implement this mapping by this code:
public class TourProfile : Profile
{
private readonly GoodTripDbContext _dbContext;
public TourProfile(GoodTripDbContext goodTripDbContext)
{
_dbContext = goodTripDbContext;
CreateMap<TourDto, Tour>()
.ForMember(dest => dest.TypeOfTransport, opt => opt.Ignore())
.ForMember(dest => dest.ToursCountries, opt => opt.Ignore())
.AfterMap((dto, tour) =>
{
tour.TypeOfTransport = _dbContext.TypesOfTransports.FirstOrDefault(tt => tt.Name.Equals(dto.TypeOfTransport));
if (dto.Countries != null && dto.Countries.Any())
{
tour.ToursCountries = new List<TourCountry>();
foreach (var countryName in dto.Countries)
{
var country = _dbContext.Countries.FirstOrDefault(c => c.Name.Equals(countryName));
if (country != null)
{
tour.ToursCountries.Add(new TourCountry { Country = country });
}
}
}
});
}
}
But it return to me this exception: "AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping."
May be someone much smarter then I can advice more correct code-example for solving this problem)
May be it will be simpler to use enum file with countries-values instead of table with entities in Db.
P.s. I know, that using DbContext on main-project level it's very bad practice, but in this example I just tried to make it work somehow.