Reputation: 587
I run out of ideas how to configure automapper to map from ViewModel class to a DTO class. the structure of the code is as follows:
public class BreedingD : CooperationSpotD
{
public string ContactPerson { get; set; }
}
public abstract class CooperationSpotD
{
public int? Id { get; set; }
public string Name { get; set; }
public DateTime? CooperationStart { get; set; }
public DateTime? CooperationEnd { get; set; }
public bool? Active { get; set; }
public AddressD Address { get; set; }
}
public class AddressD
{
public int? Id { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
public string FlatNo { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
}
public class BreedingViewModel : CooperationSpotViewModel
{
private List<int> _selectedBreeds;
private List<BreedViewModel> _allBreeds;
public string ContactPerson { get; set; }
public List<int> SelectedBreeds
{
get
{
if (_selectedBreeds == null)
_selectedBreeds = new List<int>();
return _selectedBreeds;
}
set
{
if (value != null)
_selectedBreeds = value;
}
}
public List<BreedViewModel> AllBreeds
{
get
{
if (_allBreeds == null)
_allBreeds = new List<BreedViewModel>();
return _allBreeds;
}
set
{
if (value != null)
_allBreeds = value;
}
}
}
public abstract class CooperationSpotViewModel : BaseViewModel
{
public int? Id { get; set; }
public string Name { get; set; }
public DateTime? CooperationStart { get; set; }
public DateTime? CooperationEnd { get; set; }
public bool? Active { get; set; }
public int? AddressId { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
public string FlatNo { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
}
The DataProfile part for the classes I am trying to map
CreateMap<BreedingViewModel, AddressD>()
.ConstructUsing(x => new AddressD {
City = x.City,
FlatNo = x.FlatNo,
HouseNumber = x.HouseNumber,
Postcode = x.Postcode,
Street = x.Street,
Id = x.AddressId });
CreateMap<BreedingViewModel, BreedingD>()
.ConstructUsing(x => new BreedingD
{
Active = x.Active,
ContactPerson = x.ContactPerson,
CooperationStart = x.CooperationStart,
CooperationEnd = x.CooperationEnd,
Id = x.Id,
Name = x.Name,
});
When I call mapper:
var mapped = _mapper.Map<BreedingD>(vm);
I keep on receiving error about not mapped property Address. I tried various ways of mapping. This DataProfile is the latest, slightly desparate way to do this.
Edit:
CreateMap<BreedingViewModel, BreedingD>()
.ConstructUsing(x => new BreedingD
{
Active = x.Active,
ContactPerson = x.ContactPerson,
CooperationStart = x.CooperationStart,
CooperationEnd = x.CooperationEnd,
Id = x.Id,
Name = x.Name,
Address = new AddressD { Id = x.AddressId, City = x.City, Street = x.Street, Postcode = x.Postcode, HouseNumber = x.HouseNumber, FlatNo = x.FlatNo }
});
This DataProfile configuration doesn't work either.
The exception message is:
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 AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. BreedingViewModel -> BreedingD (Destination member list) SeeingEyeDog.Models.BreedingViewModel -> SeeingEyeDog.BusinessLogic.Models.BreedingD (Destination member list)
Unmapped properties: Address
Upvotes: 0
Views: 449
Reputation: 587
Ok, I figured it. Posting solution for future for people who might run into such a stupid situation. The issue was not with the mapping itself. I have 2 DataProfile files, one in BusinessLogic Project and the other one in the project holiding the controllers. Only the one in Business Logic project was registerd in Startup.cs. Registering both profiles solved the issue.
Upvotes: 0