Reputation: 177
My project compiles and runs - just wont map from one type to another during run time. All code and error message shown below. Does anyone have any ideas / input on this? Thanks in advance!
I have the following code in my startup.cs (configure method) :-
Mapper.Initialize(config =>
{
config.CreateMap<VehicleViewModel, Vehicle>().ReverseMap();
});
which in my mind creates a two way mapping between VehicleViewModel and Vehicle.
the code for VehicleViewMOdel is listed below :-
public class VehicleViewModel
{
//Fuel Economy is in L/Km
public float CityFuelEconomy {get;set;}
public float HighwayFuelEconomy {get;set;}
public float ListPrice {get;set;}
public float SoldPrice{get;set;}
[Required]
public int ModelForeignKey {get;set;}
[Required]
public int DealerForeignKey{get;set;}
public string Notes{get; set;}
public string Color{get; set;}
}
For completeness, the code for Vehicle is listed below :-
public class Vehicle
{
[Key]
public int Id {get; set;}
//Fuel Economy is in L/Km
public float CityFuelEconomy {get;set;}
public float HighwayFuelEconomy {get;set;}
public float ListPrice {get;set;}
public float SoldPrice{get;set;}
[ForeignKey("VehicleModelId")]
public virtual VehicleModels ModelForeignKey {get;set;}
[ForeignKey("VehicleDealerId")]
public virtual Dealer DealerForeignKey{get;set;}
public string Notes{get; set;}
public string Color{get; set;}
}
when the mapping is executed, I see the following message :-
Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
EDIT - as per Hien Nguyen's useful suggestion, I have used ForMember to map the members that are of different types :-
Mapper.Initialize(config =>
{
config.CreateMap<VehicleViewModel, Vehicle>()
.ForMember(dest => dest.DealerForeignKey, opt => opt.MapFrom<DealerResolver>())
.ForMember(dest => dest.ModelForeignKey, opt => opt.MapFrom<VehicleModelResolver>()).ReverseMap();
});
this will use my resolvers to take the relevent Id from the view model and retrieve the correct object that is needed by the model. Here is the code that does the business in the DealerResolver for example :-
public Dealer Resolve(VehicleViewModel source, Vehicle destination, Dealer destMember, ResolutionContext context)
{
List<Dealer> dealers = _vehicleRepo.GetDealerById(source.DealerForeignKey).ToList();
return dealers.FirstOrDefault();
}
I used a very similar idea for the VehicleModelResolver.
So far so good.
The only issue is that when I run the code and try to do the mapping I now see a different error message all together :-
An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll Innermost exception System.MissingMethodException : No parameterless constructor defined for this object
Do you have any idea of what is causing this? I genuinley feel that the orig suggestion of using 'ForMember' is the correct way to go and think we're nearly at a solution for this.
Upvotes: 1
Views: 1422
Reputation: 18975
Your model and viewmodel have properties with different type. You can use ForMember to custom mapping between property.
Refer this for custom mapping property How to handle custom Properties in AutoMapper
Upvotes: 1