How to resolve 'Nullable object must have a value' error in Automapper and EF Core?

Cenk 1,041 Reputation points
2024-01-11T17:11:22.23+00:00

I am working on a Blazor Server application in which I added a new nullable field called ShippingWeek to an entity called OrderDetail. However, when I try to use Automapper to map an Order entity to its corresponding OrderDto entity, I get a System.InvalidOperationException: Nullable object must have a value error. Here's the code I'm using in OnInitializedAsync:

IQueryable<OrderDto?> _ordersDto = Mapper.ProjectTo<OrderDto>(_order);

And here's the relevant portion of the Automapper profile:

CreateMap<Order, OrderDto>()
    .ForMember(dest =>
            dest.OrderDetailsDto,
        opt => opt.MapFrom(src => src.OrderDetails))
    .ForMember(dest =>
            dest.Customer,
        opt => opt.MapFrom(src => src.Customer))
    .ForAllMembers(opts =>
    {
        opts.Condition((src, dest, srcMember) => srcMember != null);
    });

How can I resolve this error and use Automapper to map the two entities correctly?

Developer technologies | .NET | Blazor
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Cenk 1,041 Reputation points
    2024-01-11T19:15:52.97+00:00

    Here is how I fixed.

    CreateMap<Order, OrderDto>()
        .ForPath(dest =>
                dest.OrderDetailsDto,
            opt => opt.MapFrom(src => src.OrderDetails))
        .ForMember(dest =>
                dest.Customer,
            opt => opt.MapFrom(src => src.Customer))
        .ForAllMembers(opts =>
        {
            opts.PreCondition((src, dest, srcMember) => srcMember != null);
        });
    
    2 people found this answer helpful.
    0 comments No comments

  2. Pinaki Ghatak 5,690 Reputation points Microsoft Employee Volunteer Moderator
    2024-01-11T17:50:35.24+00:00

    Hello Cenk

    One way to resolve this issue is to use PreCondition instead of Condition in your AutoMapper profile. PreCondition checks the condition before the mapping operation, whereas Condition checks after the source value has been resolved. This means that if the PreCondition is not met, AutoMapper will not attempt to map the property, avoiding the Nullable object must have a value error.

    CreateMap<Order, OrderDto>()
        .ForMember(dest =>
                dest.OrderDetailsDto,
            opt => opt.MapFrom(src => src.OrderDetails))
        .ForMember(dest =>
                dest.Customer,
            opt => opt.MapFrom(src => src.Customer))
        .ForAllMembers(opts =>
        {
            opts.PreCondition((src, dest, srcMember) => srcMember != null);
        });
    
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.