title: EF Core executing stored procedure with null reference
body: <p>Executing a stored procedure creates this error with EF Core 2.0. I am beside myself with this error. Any pointers?</p>
  
  <pre><code>public class SalesDTO :  BaseEntity
  {
      public int SalesID { get; set; }
      [Display(Name = "User")]
      public string SalesRep { get; set; }
      [Display(Name = "Sales Date")]
      public DateTime OrderDate { get; set; }
      public string CustomerName { get; set; }
      public int? CustomerID { get; set; }
      [Display(Name = "Sale Center")]
      public string CenterName { get; set; }
      public int CenterID { get; set; }
      public decimal TotalAmount { get; set; }
      public SalesType SalesType { get; set; }
  }
  </code></pre>
  
  <p>Model class for the stored procedure.</p>
  
  <p>This is my implementation class</p>
  
  <pre><code>public IEnumerable&lt;SalesDTO&gt; GetSales(int centerID, string user)
  {
          string salesRep = user ?? "";
  
          List&lt;SqlParameter&gt; parms = new List&lt;SqlParameter&gt;
          {
              new SqlParameter("@CenterID", centerID),
              new SqlParameter("@SalesRep", salesRep)
          };
  
          return DataContext.Set&lt;SalesDTO&gt;().FromSql("spSalesList @CenterID, @SalesRep", parms.ToArray())
                   .Select(x =&gt; new SalesDTO
                   {
                       Id = x.SalesID,
                       SalesID = x.SalesID,
                       CustomerID = x.CustomerID,
                       CustomerName = x.CustomerName,
                       SalesType = x.SalesType,
                       TotalAmount = x.TotalAmount,
                       SalesRep = x.SalesRep,
                       CenterName = x.CenterName,
                       OrderDate = x.OrderDate,
                       CenterID = x.CenterID
                   }).AsNoTracking().ToList();
      }
  </code></pre>
  
  <p>This code throws:</p>
  
  <blockquote>
    <p>NullReferenceException: Object reference not set to an instance of an object. </p>
  </blockquote>
  
  <p>with this stack trace</p>
  
  <p><a href="https://i.stack.imgur.com/OFNGb.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OFNGb.gif" alt="Stack Trace"></a></p>
  
  <p>However, if I use SQL Server profiler, I can see that the stored procedure successfully executes when I copy what was executed back to SQL Server Management Studio and execute it.</p>