Reputation: 65
I am projecting the result of an entity framework database call into a new object type.
await _db.Type1
.ProjectTo<Type2>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);
The Type2 object has the following field on it where the type is an Enum.
CategoryType CategoryType {get; set;}
Type 1 has a virtual member with the same name, but the type is not an enum, it's a separate object type.
public virtual CategoryType CategoryType { get; set; }
Type1.CategoryType
is an object with additional information that relates to the enum.
The problem is, AutoMapper ProjectTo is automatically trying to map Type1.CategoryType
to Type2.Category Type
and throwing an error Unable to map expression from Type1.CategoryType to Type2.CategoryType)
. This makes sense because they are different object types.
I am already mapping a different field to Type2.CategoryType
in the mapping configuration using the following mapping technique.
.ForCtorParam(nameof(Type2.CategoryType), opt => opt.MapFrom(src => src.CategoryTypeId))
But how do I prevent AutoMapper from trying to map Type1.CategoryType
to Type2.CategoryType
even though the fields have the same name?
I tried using .ForSourceMember(x => x.CategoryType, opt => opt.DoNotValidate());
but AutoMapper is still trying to map the two types.
IS there a way to accomplish this without changing the names of one of the fields?
Upvotes: 0
Views: 180