Reputation: 2632
I'm getting following error message when trying to cast DTO to Model.
MEMBER_Model model = Mapper.Map<MEMBER_Model>(item);
Error mapping types.
Mapping types: T_MEMBER -> MEMBER_Model TL.CFM.DATA.T_MEMBER -> TL.CFM.CORE.MEMBER_Model
Type Map configuration: T_MEMBER -> MEMBER_Model TL.CFM.DATA.T_MEMBER -> TL.CFM.CORE.MEMBER_Model
Destination Member: MEMBER_GROUPs
DTO Class:
public partial class T_MEMBER
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public T_MEMBER()
{
this.REL_MEMBER_GROUP = new HashSet<REL_MEMBER_GROUP>();
}
public decimal ID { get; set; }
public string USERNAME { get; set; }
public string PASSWORD { get; set; }
public decimal IS_ACTIVE { get; set; }
public decimal IS_DELETED { get; set; }
public Nullable<decimal> CRE_BY { get; set; }
public Nullable<System.DateTime> CRE_DATE { get; set; }
public Nullable<decimal> UPD_BY { get; set; }
public Nullable<System.DateTime> UPD_DATE { get; set; }
public decimal PERSON_ID { get; set; }
}
Model Class:
public class MEMBER_Model : _BaseModel
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public MEMBER_Model()
{
this.MEMBER_GROUPs = new HashSet<MEMBER_GROUP_Model>();
}
public override decimal ID { get; set; }
public string USERNAME { get; set; }
public string PASSWORD { get; set; }
public bool IS_ACTIVE { get; set; }
public bool IS_DELETED { get; set; }
public override Nullable<decimal> CRE_BY { get; set; }
public override Nullable<DateTime> CRE_DATE { get; set; }
public override Nullable<decimal> UPD_BY { get; set; }
public override Nullable<DateTime> UPD_DATE { get; set; }
public decimal PERSON_ID { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MEMBER_GROUP_Model> MEMBER_GROUPs { get; set; }
public string Fullname
{
get
{
return string.Format("{0} {1}", FIRST_NAME, LAST_NAME);
}
}
}
AutoMapper Configs:
public static void SetAutoMapperConfs()
{
Mapper.Initialize(cfg =>
{
#region MEMBER_Model -1
cfg.CreateMap<T_MEMBER, MEMBER_Model>()
.ForMember(d => d.MEMBER_GROUPs, f => f.MapFrom(src => src.REL_MEMBER_GROUP))
.ReverseMap();
#endregion
#region MEMBER_GROUP_Model -2
cfg.CreateMap<REL_MEMBER_GROUP, MEMBER_GROUP_Model>()
.ForMember(d => d.AUTH_GROUP, f => f.MapFrom(src => src.LKP_AUTH_GROUP))
.ForMember(d => d.MEMBER, f => f.MapFrom(src => src.T_MEMBER))
.ReverseMap();
#endregion
#region AUTH_GROUP_Model -3
cfg.CreateMap<LKP_AUTH_GROUP, AUTH_GROUP_Model>()
.ForMember(d => d.GROUP_ROLEs, f => f.MapFrom(src => src.REL_GROUP_ROLE))
.ForMember(d => d.MEMBER_GROUPs, f => f.MapFrom(src => src.REL_MEMBER_GROUP))
.ReverseMap();
#endregion
#region GROUP_ROLE_Model -4
cfg.CreateMap<REL_GROUP_ROLE, GROUP_ROLE_Model>()
.ForMember(d => d.AUTH_GROUP, f => f.MapFrom(src => src.LKP_AUTH_GROUP))
.ForMember(d => d.ROLE, f => f.MapFrom(src => src.LKP_ROLE))
.ReverseMap();
#endregion
#region ROLE_Model -5
cfg.CreateMap<LKP_ROLE, ROLE_Model>()
//.ForMember(d => d.GROUP_ROLEs, f => f.MapFrom(src => src.REL_GROUP_ROLE))
.ReverseMap();
#endregion
});
}
Numbers at #region labels showing relation flow.
As you can see, #region ROLE_Model -5 has a commented line. This line causes the problem. (Note: I think recursion starts here first time and it maybe triggers problem)
=UPDATED=
OutPut Values:
Exception thrown: 'AutoMapper.AutoMapperConfigurationException' in AutoMapper.dll frknc: AutoMapper.AutoMapperConfigurationException: 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. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)
Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER
at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll frknc: AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model
Type Map configuration: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model
Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperConfigurationException: 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. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)
Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER
at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) --- End of inner exception stack trace ---
at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll frknc: AutoMapper.AutoMapperMappingException: Error mapping types.Mapping types: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model
Type Map configuration: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model
Destination Member: ROLE ---> AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model
Type Map configuration: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model
Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperConfigurationException: 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. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)
Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER
at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) --- End of inner exception stack trace ---
at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) at AutoMapper.Mapper.AutoMapper.IRuntimeMapper.Map[TSource,TDestination](TSource source, TDestination destination, ResolutionContext context, IMemberMap memberMap) at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll frknc: AutoMapper.AutoMapperMappingException: Error mapping types.Mapping types: LKP_AUTH_GROUP -> AUTH_GROUP_Model TL.CFM.DATA.LKP_AUTH_GROUP -> TL.CFM.CORE.AUTH_GROUP_Model
Type Map configuration: LKP_AUTH_GROUP -> AUTH_GROUP_Model TL.CFM.DATA.LKP_AUTH_GROUP -> TL.CFM.CORE.AUTH_GROUP_Model
Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model
Type Map configuration: REL_GROUP_ROLE -> GROUP_ROLE_Model TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.GROUP_ROLE_Model
Destination Member: ROLE ---> AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model
Type Map configuration: LKP_ROLE -> ROLE_Model TL.CFM.DATA.LKP_ROLE -> TL.CFM.CORE.ROLE_Model
Destination Member: GROUP_ROLEs ---> AutoMapper.AutoMapperConfigurationException: 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. REL_GROUP_ROLE -> MEMBER_GROUP_Model (Destination member list) TL.CFM.DATA.REL_GROUP_ROLE -> TL.CFM.CORE.MEMBER_GROUP_Model (Destination member list)
Unmapped properties: ID MEMBER_ID AUTH_GROUP MEMBER
at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) --- End of inner exception stack trace ---
at lambda_method(Closure , LKP_ROLE , ROLE_Model , ResolutionContext ) at AutoMapper.Mapper.AutoMapper.IRuntimeMapper.Map[TSource,TDestination](TSource source, TDestination destination, ResolutionContext context, IMemberMap memberMap) at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , LKP_AUTH_GROUP , AUTH_GROUP_Model , ResolutionContext ) Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll
Upvotes: 2
Views: 37324
Reputation: 2337
The Automapper will throw error for unused dto object keys while mapping.
The following fixed the problem. Add this to your profile.
.ForAllOtherMembers(x => x.Ignore())
Upvotes: 2
Reputation: 1547
Did you try ignoring them in the profile and seeing if the error persists? If it persists did you try to look into exception details? It should say which members are not mapped? ForAllOtherMemebers(x=>x.Ignore()). More info: dotnettutorials.net/lesson/ignore-using-automapper-in-csharp
On top of that you cannot Map ICollection to ICollection as they are not concrete type. For example:
The soure can be an IEnumerable but the output can be only a List.
Upvotes: 3