AutoMapper / AutoMapper.Extensions.ExpressionMapping

MIT License
143 stars 39 forks source link

Exception "The source and destination types must be the same" when converting nullable property to non Nullable #143

Closed ahmedeltabee closed 2 years ago

ahmedeltabee commented 2 years ago

can't convert nullable to non nullable using expression mapping here is the code : Model { public Guid Id { get; set; } } Dto {public Guid? Id { get; set; } }

Service layer { public async Task GetFirstOrDefaultAsync(Expression<Func<TDto, bool>> dtoPredicate) { var predicate = Mapper.MapExpression<Expression<Func<T, bool>>>(dtoPredicate); var result = await Repository.FirstOrDefaultAsync(predicate); var dtoResult = Mapper.Map(result); return dtoResult ; } }

expection : Message =The source and destination types must be the same for expression mapping between literal types. Source Type: Guid, Source Description: Id, Destination Type: Nullable`1, Destination Property: Id.

BlaiseD commented 2 years ago

That's an expected exception thrown by this library.

Here's how it is designed to be used.