title: The cast to value type failed because the materialized value is null
body: <p>I'm using AutoMapper to project my entities to models.</p>
  
  <p>These are the models I'mm mapping to and from:</p>
  
  <pre><code>public partial class Material
  {
      public System.Guid Id { get; set; }
      public string Description { get; set; }
      public string EAN { get; set; }
  
      public virtual InventoryLine InventoryLine { get; set; }
  }
  
  public partial class InventoryLine
  {
      public System.Guid MaterialId { get; set; }
      public Nullable&lt;decimal&gt; Quantity { get; set; }
      public decimal Price { get; set; }
      public Nullable&lt;System.DateTime&gt; LastInspectionDate { get; set; }
      public int TransactionsSinceLastInspection { get; set; }
  
      public virtual Material Material { get; set; }
  }
  
  public class InventoryLineViewModel
  {
      public string EAN { get; set; }
      public string Description { get; set; }
      public decimal Price { get; set; }
      public decimal? Quantity { get; set; }
      public DateTime? LastInspectionDate { get; set; }
  }
  </code></pre>
  
  <p>I have this mapping:</p>
  
  <pre><code>CreateMap&lt;Material, InventoryLineViewModel&gt;().ForMember(d =&gt; d.Price, o =&gt; o.MapFrom(s =&gt; s.InventoryLine.Price)).ForMember(d =&gt; d.Quantity, o =&gt; o.MapFrom(s =&gt; s.InventoryLine.Quantity)).ForMember(d =&gt; d.LastInspectionDate, o =&gt; o.MapFrom(s =&gt; s.InventoryLine.LastInspectionDate));
  </code></pre>
  
  <p>Whenever I run the this code:</p>
  
  <pre><code>Mapper.Initialize(c =&gt; { c.AddProfile(new MapperProfile()); });
  return context.Material.Include(i =&gt; i.InventoryLine).ProjectTo&lt;InventoryLineViewModel&gt;().ToList();
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.</p>
  </blockquote>
  
  <p>How is that possible when all of the types that I am mapping to and from are of the same datatypes? I have even tried making the <code>Quantity</code> property a non-nullable in the database and in the view-model. I still get the same error.</p>
  
  <p>Any help is appreciated :-)</p>