SkyeBoniwell
SkyeBoniwell

Reputation: 7092

AutoMapper -- AutoMapperMappingException: Error mapping types

I am getting this error:

AutoMapperMappingException: Error mapping types.

Mapping types: Game -> VirtualGame

InvalidOperationException: Nullable object must have a value.

Property: Timing lambda_method(Closure , object , object , ResolutionContext )

AutoMapperMappingException: Error mapping types.

I think I've traced it down to the code block below. Is there a way to check for null in this block so that this error goes away?

cfg.CreateMap<Game, VirtualGame>()

        .ForMember(d => d.GameTiming, opt =>
                {

                    opt.Condition(s => s.GameStartTime != null && s.GameEndTime != null);
                    opt.MapFrom(
                        s => new Timing(s.GameStartTime.Value, s.GameEndTime.Value, s.GameDuration));
                })

Oh, and this is what "Timing" is:

public Timing(DateTime gameStartTime, DateTime gameEndTime, Int32?
              gameDuration = null)

Upvotes: 0

Views: 2995

Answers (1)

Lucian Bargaoanu
Lucian Bargaoanu

Reputation: 3516

opt.PreCondition(s => s.GameStartTime != null && s.GameEndTime != null);

The docs.

Similarly, there is a precondition. The difference is that it runs sooner in the mapping process, before the source value is resolved (think MapFrom or ResolveUsing). So the precondition is called, then we decide which will be the source of the mapping (resolving), then the condition is called and finally the destination value is assigned. You can see the steps yourself.

Upvotes: 4

Related Questions