Automapper 2 source fields mapped to 1 destination field - c#

I have 2 classes to be mapped:
class1 has fields PaymentState and PaymentStateId
public int PaymentStateId { get; set; }
[ForeignKey(nameof(PaymentStateId))]
[InverseProperty(nameof(PaymentStateEntity.OrderEntities))]
public virtual PaymentStateEntity PaymentState { get; set; }
class2 has field with the same name PaymentState but of enum type
public PaymentState PaymentState { get; set; }
while mapping class1 to class2 there is an error that field PaymentState cannot be mapped:
Unable to create a map expression from
class1.PaymentState (Entities.PaymentStateEntity) to PaymentState.PaymentState (Enums.PaymentState)
Mapping types:
class1-> class2
Destination Member:
PaymentState
have tried custom mapping fields, but I guess that the fact that there are 2 fields now which are to be mapped to 1 destication field makes the problem
CreateMap<class1, class2>()
.ForMember(dest => dest.PaymentState, opt => opt.MapFrom(src => src.PaymentStateId))
What is the way to ignore one source field though let another source field to be mapped to destination field?

I don't think the error cause is 2 fields mapped into 1 destination. Probably because of the difference of type between dest and source. You should check log what the error is.
But you can try to use
CreateMap<class1, class2>()
.ForMember(dest => dest.PaymentState, opt => opt.Ignore())
.ForMember(dest => dest.PaymentState, opt => opt.MapFrom(src => src.PaymentStateId))
Besides, you can convert 2 enums by setting their items to have the same value
.ForMember(dest => dest.PaymentState, opt => opt.MapFrom(src => (PaymentState)((int)src.PaymentState)))

Related

Automapper: use default mapping for all other members

I have a lot of classes to map from IDataReader like this:
class Dest
{
public string Field1;
public string Field2;
public string Field3;
}
...
public DestProfile
{
CreateMap<IDataReader, Dest>()
.ForMember(d => d.Field1, opt => opt.MapFrom(/*Do some custom convertion here*/)
.ForAllOtherMembers(/*Do default convention-based mapping for all other members/*)
}
So i'd like to perform custom convertion on selected fields and do default mapping without explicitly coding.
Question looks very common, but I didn't find how to achieve this.
So the most straightforward way is to write like this:
.ForAllOtherMembers(opt => opt.MapFrom(src => src[opt.DestinationMember.Name]))
But there are some caveats. For example
.IncludeBase<IDataReader, Base>()
.ForAllOtherMembers(opt => opt.MapFrom(src => src[opt.DestinationMember.Name]))
Here ForAllOtherMembers will override your base class definition.

Automapper Map from outer class to inner class variable

I'm using Automapper and need to know whether I can map a source variable to a nested member variable?
This is the source I want to map from
public class source
{
public string name;
}
This is the destination - I need the name variable assigned to the Nested.Name member
public class Destination
{
public Nested info;
}
public class Nested
{
public string name;
}
Any help greatly appreciated.
Ron.
ForPath would do the trick
CreateMap<source, Destination>()
.ForPath(d => d.info.name, c => c.MapFrom(src => src.name));
Something like this should do the trick, although you might run into an uninitialized dst.info
CreateMap<source, Destination>()
//reegular mapping here
.ForMember(dst => dst.foo, c => c.MapFrom(src => src.otherfoo))
//AfterMap to bind your properties
.AfterMap((src, dst) => { dst.info.name = src.name; });

Automapper - exclude some objects from mapped collection

I have the following map rules:
CreateMap<ViewModels.ApplicationDriverAccidentFormVM, ApplicationDriverAccidentDomain>();
then I want to map ViewModels.ApplicationDriverFormVM to ApplicationDriverDomain, both are have Accidents property, which are appropriate collections for each type.
public class ApplicationDriverDomain
{
public List<ApplicationDriverAccidentDomain> Accidents { get; set; }
}
public class ApplicationDriverFormVM
{
public List<ApplicationDriverAccidentFormVM> Accidents { get; set; }
}
And I want to exclude (not map) all records, which are not satisfied some conditions
I try to write the following code:
CreateMap<ViewModels.ApplicationDriverFormVM, ApplicationDriverDomain>()
.ForMember(dest => dest.Accidents, opt => opt.MapFrom(src => GetNotNullFromCollection(src.Accidents)))
where GetNotNullFromCollection is:
List<object> GetNotNullFromCollection(object input)
{
List<object> output = new List<object>();
foreach (var item in (List<object>)input)
{
if (!Utils.IsAllNull(item))
output.Add(item);
}
return output;
}
but it says me:
Unable to cast object of type
'System.Collections.Generic.List1[Web.ViewModels.ApplicationDriverAccidentFormVM]'
to type 'System.Collections.Generic.List1[System.Object]'.
Why and how to do it?
your method GetNotNullFromCollection receives an object but you are passing it a list.
Anyway, I would recommend using Generics instead of objects.
I solved it by the following way:
CreateMap<ViewModels.ApplicationDriverFormVM, ApplicationDriverDomain>().ForMember(dest => dest.Accidents, opt => opt.MapFrom(src => src.Accidents.Where(o => !Utils.IsAllNull(o))))

Automapper 5.1.1 inheritance mapping

Hello I cant get my mappings working for inherited class.
Idea is to create map for base object and interface only once, and when child classes implements their own members, configure mapping nly for those members that are not defined in base class or intrface.
Let me begin with sample code.
public class DtoClass {
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
}
public interface IField3 {
public string EntityField3 { get; set; }
}
public class BaseEntityClass {
public string EntityField1 { get; set; }
}
public class ChildEntityClass : BaseEntityClass, IField3 {
public string EntityField2 { get; set; }
public string EntityField3 { get; set; }
}
CreateMap<BaseEntityClass, DtoClass>()
.ForMember(c => c.Field1 , m => m.MapFrom(a => a.EntityField1))
.Include<ChildEntityClass, DtoClass>();
CreateMap<IField3, DtoClass>()
.ForMember(c => c.Field3 , m => m.MapFrom(a => a.EntityField3));
CreateMap<ChildEntityClass, DtoClass>()
.ForMember(c => c.Field2 , m => m.MapFrom(a => a.EntityField2));
Attached code dosnt work ofcourse. when calling :
AutoMapper.Mapper.Map<ChildEntityClass, DtoClass>(instanceOfChildEntityClass);
I get only mapped members that are defined in CreateMap<ChildEntityClass, DtoClass>().
Any idea how to implement mappings for base class and interfaces only once?
And yes i want to map all types ChildEntityClass, BaseEntityClass and IField3 to DtoClass.
Any hints are welcome for elegant configuration such mappings.
Edit: I remove unecssary IncludeBase from subclass for clarity, but none of both
- IncludeBase in subclass
- Include in base class
Works for me. What can cause such problems ?
You should not be using .IncludeBase AND .Include - pick one and stick with it. I prefer .IncludeBase, as I think it makes more sense to define in the subclass. In your case, you cannot reference IField3 using Include because there is no implicit conversion.
The following code works using IncludeBase for me
CreateMap<BaseEntityClass, DtoClass>()
.ForMember(dest => dest.Field1, opt => opt.MapFrom(src => src.EntityField1))
;
CreateMap<IField3, DtoClass>()
.ForMember(dest => dest.Field3, opt => opt.MapFrom(src => src.EntityField3))
;
CreateMap<ChildEntityClass, DtoClass>()
.ForMember(dest => dest.Field2, opt => opt.MapFrom(src => src.EntityField2))
.IncludeBase<BaseEntityClass, DtoClass>()
.IncludeBase<IField3, DtoClass>()
;
As it often happens issue were outside the scope i delivered in sample code.
In my project in initialization method was hidden invocation of something like:
foreach (string propName in map.GetUnmappedPropertyNames())
{
expr.ForMember(propName, opt => opt.Ignore());
}
So all columns not mapped in subclass were automatically ignored even when invoking mapping for base type. Simple yet problematic.
Such code as above were probably aded for Mapper.Configuration.AssertConfigurationIsValid(); to pass.

AutoMapper use parent class mapping in child classes

I've got two parent classes
class Settings
{
public Field
}
and
class SettingsDb
{
public FieldDB
}
and Field mapped width FieldDB by ForMember expression
CreateMap<SettingsDb, Settings>()
.ForMember(dest => dest.Field,
opt => opt.MapFrom(src => some lambda with src.FieldDb));
I also have two child classes
class AdminSettings : Setting
{
inherits Field
}
class AdminSettingsDb : SettingDb
{
inherits FieldDb
}
how can I map AdminSettings with AdminSettingsDb without doing ForMember again for this inherited fields?
CreateMap<AdminSettings, AdminSettingsDb>();
now this fields are empty after mapping
Simpler solution
CreateMap<BaseEntity, BaseDto>()
.ForMember(dest => dest.SomeMember, opt => opt.MapFrom(src => src.OtherMember));
CreateMap<DerivedEntity, DerivedDto>()
.IncludeBase<BaseEntity, BaseDto>();
Include the child classes in the mapping of the parent.
CreateMap<SettingsDb, Settings>()
.Include<AdminSettings, AdminSettingsDb>()
.ForMember(dest => dest.Field,
opt => opt.MapFrom(src => some lambda with src.FieldDb));
CreateMap<AdminSettings, AdminSettingsDb>();
See Mapping inheritance.

Categories