0

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.

  • 1
    _"But I can't to implement this mapping back, from entity to dto."_ - why? it does not compile? it throws an exception at runtime? Are you using AutoMapper? – Guru Stron Jul 12 '23 at 06:58
  • @GuruStron , I use AutoMapper) It throws to me exception with this message: "AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping." – Raman Shuliak Jul 12 '23 at 07:06
  • Have you registered IMapper and this ``TourProfile `` to the DI? – sa-es-ir Jul 12 '23 at 07:33
  • @sa-es-ir , If you mean this string in Program.cs then yes. -> builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); – Raman Shuliak Jul 12 '23 at 07:36
  • So How you pass the DbContext as constructor parameter, I think you need to use other ways: https://stackoverflow.com/questions/40275195/how-to-set-up-automapper-in-asp-net-core – sa-es-ir Jul 12 '23 at 07:40
  • Please provide where you are doing the mapping and check if there are any values that are null during the process, I tested with your config and it works fine. – Chen Jul 13 '23 at 09:15

0 Answers0