title: Automapper EF Core throws "Value cannot be null. Parameter name: querySource" for navigation Properties
body: <p>I am getting <code>Value cannot be null. Parameter name: querySource</code> error from Automapper when mapping navigation properties.</p>
  
  <pre><code>.ForMember(p =&gt; p.Region, opt =&gt; opt.MapFrom(p =&gt; 
    (p.RegAddressId.HasValue &amp;&amp; p.RegAddress.CountryId.HasValue)? 
    p.RegAddress.Country.Region: p.Nationality.Region)) 
  </code></pre>
  
  <p>RegAddressId is nullable and NationalityId is not nullable.</p>
  
  <pre><code>public class User : IEntity
  {
      ...
      public int NationalityId { get; set; }
      public int? RegAddressId { get; set; }
  
      public virtual LkupNationality Nationality { get; set; }
      public virtual Address RegAddress { get; set; }
  }
  </code></pre>
  
  <p>I am using Automapper Version is 7.0.1 and EF Core 2.0.3</p>
  
  <p>Following is the expression which got from,</p>
  
  <pre><code>var expression = pendingRegs.ProjectTo&lt;PendingClientModel&gt;().Expression;
  
  Region = .If (.If (
              ($dtoUserRegistration.RegAddressId).HasValue &amp;&amp; (($dtoUserRegistration.RegAddress).CountryId).HasValue &amp;&amp; $dtoUserRegistration.RegAddress !=
              null &amp;&amp; ($dtoUserRegistration.RegAddress).Country != null
          ) {
              (($dtoUserRegistration.RegAddress).Country).Region
          } .Else {
              ($dtoUserRegistration.Nationality).Region
          } == null) {
              null
          } .Else {
              .New Cft.Fbo.Domain.ViewModels.ListItemModel(){
                  Desc = (.If (
                      ($dtoUserRegistration.RegAddressId).HasValue &amp;&amp; (($dtoUserRegistration.RegAddress).CountryId).HasValue &amp;&amp; $dtoUserRegistration.RegAddress !=
                      null &amp;&amp; ($dtoUserRegistration.RegAddress).Country != null
                  ) {
                      (($dtoUserRegistration.RegAddress).Country).Region
                  } .Else {
                      ($dtoUserRegistration.Nationality).Region
                  }).Name,
                  Id = (.If (
                      ($dtoUserRegistration.RegAddressId).HasValue &amp;&amp; (($dtoUserRegistration.RegAddress).CountryId).HasValue &amp;&amp; $dtoUserRegistration.RegAddress !=
                      null &amp;&amp; ($dtoUserRegistration.RegAddress).Country != null
                  ) {
                      (($dtoUserRegistration.RegAddress).Country).Region
                  } .Else {
                      ($dtoUserRegistration.Nationality).Region
                  }).Id
              }
          },
  </code></pre>
  
  <p>UPDATE: This is due to a bug in EF Core. I have raised an <a href="https://github.com/aspnet/EntityFrameworkCore/issues/14020#issuecomment-442191955" rel="nofollow noreferrer">issue</a> and will update the question once it is fixed. If anyone looking for a workaround, this is what I got from EF Core team.</p>
  
  <blockquote>
    <p>The work-around (in the absence of auto-mapper) would be not use null
    check on navigation. EF Core does that for you already. Just make sure
    to cast to nullable type in the end. In EFCore if you write
    dtoUser.RegAddress.Country.Region.Id it will give you null if any
    navigation is null else give you value of associated Id.</p>
  </blockquote>