I use Automapper with EntityFramework Core, and have the following configuration for one of my objects (Couche
, having associated an Affaire
and a Sol
object)
CreateMap<Couche, CoucheDTO>()
.ForMember(dto => dto.AffaireId, o => o.MapFrom(p => p.Affaire.Id))
.ForMember(dto => dto.SolId, o => o.MapFrom(p => p.Sol.Id))
.ForMember(dto => dto.SolNom, o => o.MapFrom(p => p.Sol.Nom))
.ForMember(dto => dto.SolCode, o => o.MapFrom(p => p.Sol.Code))
.ReverseMap()
.ForPath(couche => couche.Affaire, o => o.MapFrom(p => {return null; })) // ???
.ForPath(couche => couche.Sol, o => o.Ignore()); // ???
When I reverse map (from DTO to Business Object), I need only the correct AffaireId
, the Affaire
field should be set to null (I don't need to create invalid half-filled objects). How to do it with reverse map? Ignore
seem do not put null....
PS. I tried
.ForPath(couche => couche.Affaire, o => o.MapFrom<AffaireDTO>(p => null))
.ForPath(couche => couche.Sol, o => o.MapFrom<AffaireDTO>(p => null));
but get then
InvalidOperationException: No coercion operator is defined between types 'MyApp.Core.Entities.Sol' and 'MyApp.Core.Entities.Affaire'. System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
DependencyResolutionException: An exception was thrown while activating MyApp.Web.Services.SondageService -> λ:AutoMapper.IMapper -> λ:AutoMapper.IConfigurationProvider. Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action next)