Closed ErikGjers closed 1 year ago
Don't recommend mapping literals like int
with expression mapping. You probably want the following ForPath
for "deflattening"
cfg.CreateMap<Work, WorkDto>()
.ForMember(
dest => dest.PrimaryCategory,
config =>
config.MapFrom(
src =>
src.Categories.Any()
? src.Categories.Where(e => e.CategoryIsPrimary ?? false).Select(e => e.Category).Single()
: null
)
)
.ForPath(dest => dest.PrimaryCategory.Id, config => config.MapFrom(src => src.Id))
.ReverseMap();
Also see the configuration for this test for configuration without ForPath.
Hi again,
The test below triggers an exception. It is a many-to-many relationship with a single of those mapped to a property of a dto, since it has a set boolean, CategoryIsPrimary. One of the expressions generated gets translated to Work instead of Category and accessing the CategoryId causes the exception. The Exception is thrown in PrependParentMemberExpression() and I am not 100% sure where the expression is translated(perhaps incorrectly), but I figured some feedback would be good here before I investigate further.
Is there something I am doing incorrectly with the setup for this?
This is the exception: System.ArgumentException: 'Property 'Int32 CategoryId' is not defined for type 'AutoMapper.Extensions.ExpressionMapping.UnitTests.ExpressionTest+Work' (Parameter 'property')'
Any assistance is greatly appreciated, much like this library. PS: I will get on the other PR in the next couple of days, if not at the start of next week.