title: How to debug and fix 'Nullable object must have a value' within Entity Framework Core?
body: <p>I'm doing a projection in this method:</p>
  <pre><code>public async Task&lt;TradeDetail&gt; Get(int tradeId)
  {
      var firstOrDefaultAsync = await context.EvgTGTrade
          .Where(x =&gt; x.IdTrade == tradeId)
          .Select(trade =&gt; new TradeDetail
          {
              Id = trade.IdTrade,
              Code = trade.CdTrade,
              Description = trade.DeTrade,
              Comments = trade.DeComentarios,
              TypeId = trade.IdTipoTrade,
              BrokerId = trade.EvgTGBrokerTrade.FirstOrDefault().IdBroker,
              BillingCycleId = trade.IdCicloFacturacion,
              CounterpartId = trade.IdSujeto,
              StartDate = trade.FhIni,
              EndDate = trade.FhFin,
              SignatureDate = trade.FhFirma,
              Positions = trade.EvgTGPosicion.Select(pos =&gt; new Position
              {
                  Amount = pos.Cantidad,
                  Code = pos.CdPosicion,
                  Description = pos.DePosicion,
                  LogisticElement = pos.IdElemLogNavigation.DeElemLog,
                  StartDate = pos.FhIni,
                  EndDate = pos.FhFin,
                  FormulaId = pos.IdFormula,
                  Operations = pos.EvgTGOperacionCv.Select(op =&gt;
                      new Operation()
                      {
                          TypeId = op.IdTipoOperacion,
                          Fee = op.Fee,
                          Date = op.FhOperacionCv,
                          Price = op.NmPrecio ?? 0,
                          Quantity = op.NmCantidadKwh ?? 0,
                      }),
              })
          })
          .FirstOrDefaultAsync();
      return firstOrDefaultAsync;
  }
  </code></pre>
  <p>I'm getting an exception at the first line saying</p>
  <blockquote>
  <p>Microsoft.EntityFrameworkCore.Query: Error: An exception occurred while iterating over the results of a query for context type 'SampleApp.EF.MyDbContext'.
  System.InvalidOperationException: Nullable object must have a value.</p>
  </blockquote>
  <p>I've inspected the exception and the stack trace and it doesn't say anything about which is the problematic object. No clue of what's wrong.</p>
  <p>So, is there a method to know which property generates the conflict? I'm sure it's a property-column mismatch, but what if you have a a lot of properties? It's quite painful!</p>
  <h1>Update</h1>
  <p>I've already fixed the problem. It was due to an attempt of assigning null to a property in my model that was non-nullable.</p>
  <p>That said, this questions isn't about this concrete fix, but about <strong>finding a general way to debug this kind of situation</strong> (this exception) that are quite common. IMHO, the exception message is too vague. It doesn't provide any useful information of the conflicting property.</p>