Roelant M
Roelant M

Reputation: 1706

automapper mapping property stays null

it seems like i cannot get nested mappings working properly

after reading: http://docs.automapper.org/en/stable/Nested-mappings.html it should be very easily.

i have the following classes:

public class CatalogVehicle : VehicleBase
{
    public string Type { get; set; }
    public int Year { get; set; }
    public VehicleSpecification VehicleSpecification { get; set; } = new VehicleSpecification();
} //removed some properties for readability

public class VehicleSpecification
{
    public Engine Engine { get; set; } = new Engine();
    public Transmission Transmission { get; set; } = new Transmission();
    public int Co2 { get; set; }
    public int Weight { get; set; }
} //again removed some more properties (all classes)

for mapping:

CreateMap<VehicleAndQuote, CatalogVehicle>()
    .ForMember(catalogVehicle => catalogVehicle.Id,
        source => source.MapFrom(vehicleAndQuote => vehicleAndQuote.Quote.QuotationIdentifier.Trim()))
    .ForMember(catalogVehicle => catalogVehicle.Make,
        source => source.MapFrom(vehicleAndQuote => vehicleAndQuote.Vehicle.VehicleMakeName))
    .ForMember(catalogVehicle => catalogVehicle.Model,
        source => source.MapFrom(vehicleAndQuote => vehicleAndQuote.Vehicle.VehicleModelTypeName))
    .ForMember(catalogVehicle => catalogVehicle.VehicleSpecification, opt => opt.Ignore()); //removed some lines

CreateMap<VehicleAndQuote, VehicleSpecification>()
    .ForMember(vehicleSpecification => vehicleSpecification.Co2, src => src.MapFrom(vehicleAndQuote => vehicleAndQuote.Vehicle.Co2.ToSafeInt()))
    .ForMember(vehicleSpecification => vehicleSpecification.Weight, src => src.MapFrom(vehicleAndQuote => vehicleAndQuote.Vehicle.Weight.ToSafeInt()))
    .ForMember(vehicleSpecification => vehicleSpecification.Rating, opt => opt.Ignore())
    .ForMember(vehicleSpecification => vehicleSpecification.Tyres, opt => opt.Ignore()) //removed some lines as well

CreateMap<VehicleAndQuote, Engine>()
    .ForMember(engine => engine.Displacement, opt => opt.Ignore())
    .ForMember(engine => engine.Fuel, src => src.MapFrom(vehicleAndQuote => vehicleAndQuote.Vehicle.FuelType))
    .ForMember(engine => engine.Power, src => src.ResolveUsing(GetEnginePower))
    .ForMember(engine => engine.Cylinders, src => src.MapFrom(vehicleAndQuote => vehicleAndQuote.Vehicle.Cylinders));

//etc

as you can see i am ignoring the some properties because otherwise i get the unmapped properties error. After reading the article it should work, as long as you have all classes mapped.

i am calling the method like: var vehicle = _mapper.Map<VehicleAndQuote, CatalogVehicle>(vehicleAndQuote); <= this is the big class that contains all the information

so from mapping from VehicleAndQuote to CatalogVehicle -first few properties work properly, but then the mapping to the VehicleSpecification lies my problem. that one will not be populated properly...

does anyone see the problem?

Upvotes: 1

Views: 173

Answers (1)

Alex Riabov
Alex Riabov

Reputation: 9175

You need to configure mapping for nested classes instead of ignoring them:

.ForMember(catalogVehicle => catalogVehicle.VehicleSpecification, src => src.MapFrom(vehicleAndQuote => vehicleAndQuote);

instead of

.ForMember(catalogVehicle => catalogVehicle.VehicleSpecification, opt => opt.Ignore());

And the same for other mappings

Upvotes: 2

Related Questions