asp.net-core.net-coreentity-framework-core-3.1

EF Core and Automapper. Mapping relationship one-to-many


I'm currently using AutoMapper on an Project running code-first Entity Framework.

Here just simple entities and DTO's:

// DTO Profile
public class CreateOrEditProfileDto 
{        
    public string Code { get; set; }
    public string Name { get; set; }

    public List<RouteDto> Routes { get; set; }
}

// entity Profile
public class Profile
{       
    public virtual string Code { get; set; }
    public virtual string Name { get; set; }
}

// DTO Route
public class RouteDto 
{
    public string DriverName { get; set; }
    public string DriverSurname { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public int ProfileId { get; set; }
}

//entity Route
public class Route
{
    public virtual string DriverName { get; set; }
    public virtual string DriverSurname { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Email { get; set; }
    public virtual int ProfileId { get; set; }

    [ForeignKey("ProfileId")]
    public Profile ProfileFk { get; set; }
}

//Mapper
configuration.CreateMap<RouteDto, Route>().ReverseMap();
// configuration.CreateMap<CreateOrEditProfileDto, Profile>().ReverseMap();

// this type of configuration give the error written below
configuration.CreateMap<CreateOrEditProfileDto, Profile>()
     .ForMember(dest => dest, opt =>
        opt.MapFrom(src => src.Routes.Select(x =>
           new Route()
              {
                  ProfileId = x.ProfileId,
                  DriverName = x.DriverName,
                  DriverSurname = x.DriverSurname,
                  Phone = x.Phone,
                  Email = x.Email,
              }
           )
        )
     );

I'm a little bit confusing, I'm trying to map one-to-many relationship between Profile and Route, Route has a foreign key to Profile. A single profile could have more routes. So, I want to create a profile and attach the list of routes, but when I compile the solution, I get this error:

AutoMapper.AutoMapperConfigurationException: 'Custom configuration for members is only supported for top-level individual members on a type.'

Does anyone know the best way to solve this mapping?

Regards


Solution

  • Because List<RouteDto> is mapped to Profile, the type does not match. You need to add a property in Profile.

     public class Profile
    {
        public virtual string Code { get; set; }
        public virtual string Name { get; set; }
        public List<Route> Routes { get; set; }
    }
    

    The mapping attribute dest.Routes need to be specified. Then, the Routes will be automatically mapped.

      CreateMap<CreateOrEditProfileDto, EntityProfile>()
              .ForMember(dest => dest.Routes, opt =>
                 opt.MapFrom(src => src.Routes.Select(x=>
                 new Route()
                 {
                     ProfileId = x.ProfileId,
                     DriverName = x.DriverName,
                     DriverSurname = x.DriverSurname,
                     Phone = x.Phone,
                     Email = x.Email,
                 }
                 ))
              );