Requirement: ------------ I need to map the main DTO 'Definition' class with the View Model 'MembershipTypeDefinitionVM' using Auto-Mapper in Asp.net core 3.1. I was able to map all the fields except 'Level0FeeStructure' and 'Level1FeeStructure' to the 'FeeStructure' DTO. Please Note the 'Definition' entity has many-2-many relationship with the 'FeeStructure' entity, that's the reason we had to create a joining entity called 'DefinitionFeeStructure' which has one-2-one relationship with 'FeeStructure' entity. Where as in my view model i do not need the joining entity instead we want to directly map the 'FeeStructure' entity to the main view i.e. 'MembershipTypeDefinitionVM' as 'Level0FeeStructure' or 'Level1FeeStructure'.
Question:
---------
1. How do i map complex related fields like the one mention above?
2. Do i need to separately map the fields? if yes then how?
When i run the application it throws me the below exception:
------------------------------------------------------------
AutoMapper.AutoMapperMappingException
HResult=0x80131500
Message=Error mapping types.
Source=AMS.Web
StackTrace:
<Cannot evaluate the exception stack trace>
Inner Exception 1:
AutoMapperMappingException: Error mapping types.
Inner Exception 2:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Below are the entity classes and the Mapping profile.
public partial class Definition
{
public Definition()
{
DefinitionFeeStructure = new HashSet<DefinitionFeeStructure>();
MembershipDocumentRequirement = new HashSet<MembershipDocumentRequirement>();
}
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public int TypeId { get; set; }
public bool IsMembershipExpire { get; set; }
public int Tenure { get; set; }
public bool IsReferenceRequired { get; set; }
public bool IsByInvitation { get; set; }
public OrganizationLevelEnum OrgLevelId { get; set; }
public bool IsPersonalInfoRequired { get; set; }
public bool IsProfessionalInfoRequired { get; set; }
public bool IsEducationalInfoRequired { get; set; }
public DateTime CreatedDate { get; set; }
public string Abbreviation { get; set; }
public virtual ICollection<DefinitionFeeStructure> DefinitionFeeStructure { get; set; }
public virtual ICollection<MembershipDocumentRequirement> MembershipDocumentRequirement { get; set; }
}
public partial class DefinitionFeeStructure
{
public int Id { get; set; }
public int DefinitionId { get; set; }
public int FeeStructureId { get; set; }
public virtual Definition Definition { get; set; }
public virtual FeeStructure FeeStructure { get; set; }
}
public partial class FeeStructure
{
public FeeStructure()
{
DefinitionFeeStructure = new HashSet<DefinitionFeeStructure>();
FeeLineItems = new HashSet<FeeLineItem>();
}
public int Id { get; set; }
public int OrganizationLevel { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int DisplayOrder { get; set; }
public int IsPayNow { get; set; }
public virtual ICollection<DefinitionFeeStructure> DefinitionFeeStructure { get; set; }
public virtual ICollection<FeeLineItem> FeeLineItems { get; set; }
}
public class MembershipTypeDefinitionVM
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter name")]
public string Name { get; set; }
public int CategoryId { get; set; }
[Display(Name = "Membership Category")]
public string CategoryName { get; set; }
public int TypeId { get; set; }
public string TypeName { get; set; }
public OrganizationLevelEnum OrganizationLevel { get; set; }
public FeeStructureDefinitionVM Level0FeeStructure { get; set; }
public FeeStructureDefinitionVM Level1FeeStructure { get; set; }
}
public class MembershipTypeProfile: Profile
{
public MembershipTypeProfile()
{
CreateMap<Definition, MembershipTypeDefinitionVM>()
.ForMember(a => a.Level0FeeStructure, b => b.MapFrom(c => c.DefinitionFeeStructure.Select(d => d.FeeStructure)));
}
}
User contributions licensed under CC BY-SA 3.0