title: Entity Framework Core: `SqlNullValueException: Data is Null.` How to troubleshoot?
body: <p>I am using Entity Framework Core in an ASP.NET Core application and Controller action and I haven't changed something to the working code nor to the database but I can't tell what is the query performed by Entity Framework Core.</p>
  
  <p>The controller action:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[HttpGet]
  // GET: Administration/Companies
  public async Task&lt;ActionResult&gt; Index()
  {
      var users = await UserManager.Users.ToListAsync();
  
      var companyEditVMs = await DB.Companies
      .OrderBy(company =&gt; company.CompanyId == 1 
          ? "_" + company.CompanyName 
          : company.CompanyName
      )
      Select(a =&gt; new CompanyEditVM(HttpContext, a, users.Where(b =&gt; b.CompanyId == a.CompanyId)))
      .ToListAsync();
  
      return View(companyEditVMs);
  }
  </code></pre>
  
  <p>The trace</p>
  
  <pre class="lang-cs prettyprint-override"><code>SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
  System.Data.SqlClient.SqlBuffer.get_String()
  System.Data.SqlClient.SqlDataReader.GetString(int i)
  lambda_method(Closure , DbDataReader )
  Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable&lt;T&gt;+AsyncEnumerator.BufferlessMoveNext(DbContext _, bool buffer, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync&lt;TState, TResult&gt;(TState state, Func&lt;DbContext, TState, CancellationToken, Task&lt;TResult&gt;&gt; operation, Func&lt;DbContext, TState, CancellationToken, Task&lt;ExecutionResult&lt;TResult&gt;&gt;&gt; verifySucceeded, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable&lt;T&gt;+AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor&lt;T&gt;+EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
  System.Linq.AsyncEnumerable.Aggregate_&lt;TSource, TAccumulate, TResult&gt;(IAsyncEnumerable&lt;TSource&gt; source, TAccumulate seed, Func&lt;TAccumulate, TSource, TAccumulate&gt; accumulator, Func&lt;TAccumulate, TResult&gt; resultSelector, CancellationToken cancellationToken) in Aggregate.cs
  KYC.Core.Areas.Commercial.Controllers.CompaniesController.Index() in CompaniesController.cs
  -
          [HttpGet]
          // GET: Administration/Companies
          public async Task&lt;ActionResult&gt; Index()
          {
              var users = await UserManager.Users.ToListAsync();
  
              var companyEditVMs = await DB.Companies
                  .OrderBy(company =&gt; company.CompanyId == 1 
                      ? "_" + company.CompanyName 
                      : company.CompanyName
                  )
                  .Select(a =&gt; new CompanyEditVM(HttpContext, a, users.Where(b =&gt; b.CompanyId == a.CompanyId)))
                  .ToListAsync();
  lambda_method(Closure , object )
  Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable+Awaiter.GetResult()
  Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
  System.Threading.Tasks.ValueTask&lt;TResult&gt;.get_Result()
  </code></pre>
  
  <p>I even tried to do just <code>var companies = await DB.Companies.ToListAsync()</code>. and I have exactly the same exception.</p>
  
  <p>Maybe I would like to be able to fetch the query performed by EF Core to do that manually so that I can try to figure out what's wrong with the query.</p>
  
  <p>I am wondering what possibly could have happened. Especially since other entities like user or countries can still be fetched from the database.</p>
  
  <p>Any idea how can I troubleshoot the actual underlying issue?</p>
  
  <p>[EDIT]</p>
  
  <p>The only that actually changed in the "code" are the nuget references, I basically upgraded pretty much everything although it didn't break a reference in the code it seems it change how EF Core is somehow (wild guess) interpreting the database.</p>
  
  <p>I did reset my git repository to the point just before the nuget packages update happened and things work.,. I then decided to just update EntityFrameworkCore from 2.0.2 to 2.2.4 (also tried with 2.2.0 and ended up with the same result) and the issue happens again... Not sure what have changed between 2.0.2 and 2.2.0 to trigger this exception (but the model is the same just the EF Core version changed)...</p>
  
  <p>Here is the entity definition, it seems to have been auto-generated with a tool from the database table / schema:</p>
  
  <pre class="lang-cs prettyprint-override"><code>
      [Table("T_Companies")]
      public partial class Company : INotifyPropertyChanging, INotifyPropertyChanged
      {
          public override string ToString()
          {
              return CompanyId + " " + CompanyName;
          }
  
          private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(string.Empty);
  
          private int _CompanyId;
  
          private string _CompanyName;
  
          private int _CompanyTypeId;
  
          private int _CountryId;
  
          private string _CompanyVatNumber;
  
          private string _CompanyStreetAddress;
  
          private string _CompanyZipCode;
  
          private string _CompanyCity;
  
          private string _ContactLastName;
  
          private string _ContactFirstName;
  
          private bool? _Active;
  
          private int? _AccountId;
  
          private string _CallbackSalt;
  
          private int? _UserSpaceId;
  
          private string _Login;
  
          private string _Pwd;
  
          private bool _IsTechnicalAccount;
  
          private DateTime? _StatusDate;
  
          private int _BankStatusCode;
  
          private string _PivotalAccount;
  
          private CompanyType _CompanyType;
  
          private Country _Country;
  
          private bool _IsKycIdSent;
  
          #region Extensibility Method Definitions
          partial void OnLoaded();
          partial void OnCreated();
          partial void OnCompanyIdChanging(int value);
          partial void OnCompanyIdChanged();
          partial void OnCompanyNameChanging(string value);
          partial void OnCompanyNameChanged();
  
          partial void OnCompanyCityChanging(string value);
          partial void OnCompanyCityChanged();
          partial void OnCompanyZipCodeChanging(string value);
          partial void OnCompanyZipCodeChanged();
          partial void OnContactLastNameChanging(string value);
          partial void OnContactLastNameChanged();
          partial void OnActiveChanging(bool? value);
          partial void OnActiveChanged();
          partial void OnCompanyTypeIdChanging(int value);
          partial void OnCompanyTypeIdChanged();
          partial void OnCountryIdChanging(int value);
          partial void OnCountryIdChanged();
          partial void OnContactFirstNameChanging(string value);
          partial void OnContactFirstNameChanged();
          partial void OnCompanyVatNumberChanging(string value);
          partial void OnCompanyVatNumberChanged();
          partial void OnCompanyStreetAddressChanged();
          partial void OnCompanyStreetAddressChanging(string value);
          partial void OnAccountIdChanging(int? value);
          partial void OnAccountIdChanged();
          partial void OnCallbackSaltChanging(string value);
          partial void OnCallbackSaltChanged();
          partial void OnUserSpaceIdChanging(int? value);
          partial void OnUserSpaceIdChanged();
          partial void OnLoginChanging(string value);
          partial void OnLoginChanged();
          partial void OnPwdChanging(string value);
          partial void OnPwdChanged();
          partial void OnIsTechnicalAccountChanging(bool value);
          partial void OnIsTechnicalAccountChanged();
          partial void OnStatusDateChanging(DateTime? value);
          partial void OnStatusDateChanged();
          partial void OnBankStatusCodeChanging(int value);
          partial void OnBankStatusCodeChanged();
          partial void OnPivotalAccountChanging(string value);
          partial void OnPivotalAccountChanged();
          partial void OnIsKycIdSentChanging(bool value);
          partial void OnIsKycIdSentChanged();
  
          #endregion
  
          public Company()
          {
              OnCreated();
          }
  
          [Key, Column("CompanyId"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int CompanyId
          {
              get =&gt; _CompanyId;
              set
              {
                  if (_CompanyId != value)
                  {
                      OnCompanyIdChanging(value);
                      SendPropertyChanging();
                      _CompanyId = value;
                      SendPropertyChanged("CompanyId");
                      OnCompanyIdChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(1024), Column("CompanyName"), Required]
          public string CompanyName
          {
              get =&gt; _CompanyName;
              set
              {
                  if (_CompanyName != value)
                  {
                      OnCompanyNameChanging(value);
                      SendPropertyChanging();
                      _CompanyName = value;
                      SendPropertyChanged("CompanyName");
                      OnCompanyNameChanged();
                  }
              }
          }
  
          [Column("CompanyTypeId"), Required]
          public int CompanyTypeId
          {
              get =&gt; _CompanyTypeId;
              set
              {
                  if (_CompanyTypeId != value)
                  {
                      OnCompanyTypeIdChanging(value);
                      SendPropertyChanging();
                      _CompanyTypeId = value;
                      SendPropertyChanged("CompanyTypeId");
                      OnCompanyTypeIdChanged();
                  }
              }
          }
  
  
          [Column("CountryId"), Required]
          public int CountryId
          {
              get =&gt; _CountryId;
              set
              {
                  if (CountryId != value)
                  {
                      OnCountryIdChanging(value);
                      SendPropertyChanging();
                      _CountryId = value;
                      SendPropertyChanged("CountryId");
                      OnCountryIdChanged();
                  }
              }
          }
  
  
          [DataType(DataType.Text), StringLength(100), Column("CompanyCity"), Required]
          public string CompanyCity
          {
              get =&gt; _CompanyCity;
              set
              {
                  if (_CompanyCity != value)
                  {
                      OnCompanyCityChanging(value);
                      SendPropertyChanging();
                      _CompanyCity = value;
                      SendPropertyChanged("CompanyCity");
                      OnCompanyCityChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(100), Column("CompanyStreetAddress"), Required]
          public string CompanyStreetAddress
          {
              get =&gt; _CompanyStreetAddress;
              set
              {
                  if (_CompanyStreetAddress != value)
                  {
                      OnCompanyStreetAddressChanging(value);
                      SendPropertyChanging();
                      _CompanyStreetAddress = value;
                      SendPropertyChanged("CompanyStreetAddress");
                      OnCompanyStreetAddressChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(30), Column("CompanyVatNumber"), Required]
          public string CompanyVatNumber
          {
              get =&gt; _CompanyVatNumber;
              set
              {
                  if (_CompanyVatNumber != value)
                  {
                      OnCompanyVatNumberChanging(value);
                      SendPropertyChanging();
                      _CompanyVatNumber = value;
                      SendPropertyChanged("CompanyVatNumber");
                      OnCompanyVatNumberChanged();
                  }
              }
          }
  
  
          [DataType(DataType.Text), StringLength(10), Column("CompanyZipCode"), Required]
          public string CompanyZipCode
          {
              get =&gt; _CompanyZipCode;
              set
              {
                  if (_CompanyZipCode != value)
                  {
                      OnCompanyZipCodeChanging(value);
                      SendPropertyChanging();
                      _CompanyZipCode = value;
                      SendPropertyChanged("CompanyZipCode");
                      OnCompanyZipCodeChanged();
                  }
              }
          }
  
  
          [DataType(DataType.Text), StringLength(1024), Column("ContactLastName"), Required]
          public string ContactLastName
          {
              get =&gt; _ContactLastName;
              set
              {
                  if (_ContactLastName != value)
                  {
                      OnContactLastNameChanging(value);
                      SendPropertyChanging();
                      _ContactLastName = value;
                      SendPropertyChanged("ContactLastName");
                      OnContactLastNameChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(1024), Column("ContactFirstName"), Required]
          public string ContactFirstName
          {
              get =&gt; _ContactFirstName;
              set
              {
                  if (_ContactFirstName != value)
                  {
                      OnContactFirstNameChanging(value);
                      SendPropertyChanging();
                      _ContactFirstName = value;
                      SendPropertyChanged("ContactFirstName");
                      OnContactFirstNameChanged();
                  }
              }
          }
  
          [Column("Active"), Required]
          public bool? Active
          {
              get =&gt; _Active;
              set
              {
                  if (_Active != value)
                  {
                      OnActiveChanging(value);
                      SendPropertyChanging();
                      _Active = value;
                      SendPropertyChanged("Active");
                      OnActiveChanged();
                  }
              }
          }
  
          [Column("AccountId")]
          public int? AccountId
          {
              get =&gt; _AccountId;
              set
              {
                  if (_AccountId != value)
                  {
                      OnAccountIdChanging(value);
                      SendPropertyChanging();
                      _AccountId = value;
                      SendPropertyChanged("AccountId");
                      OnAccountIdChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(1024), Column("CallbackSalt")]
          public string CallbackSalt
          {
              get =&gt; _CallbackSalt;
              set
              {
                  if (_CallbackSalt != value)
                  {
                      OnCallbackSaltChanging(value);
                      SendPropertyChanging();
                      _CallbackSalt = value;
                      SendPropertyChanged("CallbackSalt");
                      OnCallbackSaltChanged();
                  }
              }
          }
  
          [Column("UserSpaceId")]
          public int? UserSpaceId
          {
              get =&gt; _UserSpaceId;
              set
              {
                  if (_UserSpaceId != value)
                  {
                      OnUserSpaceIdChanging(value);
                      SendPropertyChanging();
                      _UserSpaceId = value;
                      SendPropertyChanged("UserSpaceId");
                      OnUserSpaceIdChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(1024), Column("Login")]
          public string Login
          {
              get =&gt; _Login;
              set
              {
                  if (_Login != value)
                  {
                      OnLoginChanging(value);
                      SendPropertyChanging();
                      _Login = value;
                      SendPropertyChanged("Login");
                      OnLoginChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(1024), Column("Pwd")]
          public string Pwd
          {
              get =&gt; _Pwd;
              set
              {
                  if (_Pwd != value)
                  {
                      OnPwdChanging(value);
                      SendPropertyChanging();
                      _Pwd = value;
                      SendPropertyChanged("Pwd");
                      OnPwdChanged();
                  }
              }
          }
  
          [Column("IsTechnicalAccount"), Required]
          public bool IsTechnicalAccount
          {
              get =&gt; _IsTechnicalAccount;
              set
              {
                  if (_IsTechnicalAccount != value)
                  {
                      OnIsTechnicalAccountChanging(value);
                      SendPropertyChanging();
                      _IsTechnicalAccount = value;
                      SendPropertyChanged("IsTechnicalAccount");
                      OnIsTechnicalAccountChanged();
                  }
              }
          }
  
          [DataType(DataType.DateTime), Column("StatusDate")]
          public DateTime? StatusDate
          {
              get =&gt; _StatusDate;
              set
              {
                  if (_StatusDate != value)
                  {
                      OnStatusDateChanging(value);
                      SendPropertyChanging();
                      _StatusDate = value;
                      SendPropertyChanged("StatusDate");
                      OnStatusDateChanged();
                  }
              }
          }
  
          [Column("BankStatusCode")]
          public int BankStatusCode
          {
              get =&gt; _BankStatusCode;
              set
              {
                  if (_BankStatusCode != value)
                  {
                      OnBankStatusCodeChanging(value);
                      SendPropertyChanging();
                      _BankStatusCode = value;
                      SendPropertyChanged("BankStatusCode");
                      OnBankStatusCodeChanged();
                  }
              }
          }
  
          [DataType(DataType.Text), StringLength(255), Column("PivotalAccount")]
          public string PivotalAccount
          {
              get =&gt; _PivotalAccount;
              set
              {
                  if (_PivotalAccount != value)
                  {
                      OnPivotalAccountChanging(value);
                      SendPropertyChanging();
                      _PivotalAccount = value;
                      SendPropertyChanged("PivotalAccount");
                      OnPivotalAccountChanged();
                  }
              }
          }
  
          public List&lt;Resultat&gt; Resultats { get; set; }
  
          public CompanyType CompanyType
          {
              get =&gt; _CompanyType;
              set
              {
                  var previousValue = _CompanyType;
                  if (previousValue != value)
                  {
                      SendPropertyChanging();
                      _CompanyType = value;
                      if (value != null)
                      {
                          CompanyTypeId = value.CompanyTypeId;
                      }
                      else
                      {
                          _CompanyTypeId = default;
                      }
                      SendPropertyChanged("CompanyType");
                  }
              }
          }
  
          public Country Country
          {
              get =&gt; _Country;
              set
              {
                  var previousValue = _Country;
                  if (previousValue != value)
                  {
                      SendPropertyChanging();
                      _Country = value;
                      _CountryId = value?.CountryId ?? default;
                      SendPropertyChanged("Country");
                  }
              }
          }
  
          [Column("IsKycIdSent"), Required]
          public bool IsKycIdSent
          {
              get =&gt; _IsKycIdSent;
              set
              {
                  if (_IsKycIdSent != value)
                  {
                      OnIsKycIdSentChanging(value);
                      SendPropertyChanging();
                      _IsKycIdSent = value;
                      SendPropertyChanged("IsKycIdSent");
                      OnIsKycIdSentChanged();
                  }
              }
          }
  
          public event PropertyChangingEventHandler PropertyChanging;
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected virtual void SendPropertyChanging()
          {
              PropertyChanging?.Invoke(this, emptyChangingEventArgs);
          }
  
          protected virtual void SendPropertyChanged(string propertyName)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
  
          private void Attach_Resultats(Resultat entity)
          {
              SendPropertyChanging();
              entity.Company = this;
          }
  
          private void Detach_Resultats(Resultat entity)
          {
              SendPropertyChanging();
              entity.Company = null;
          }
      }
  </code></pre>
  
  <p>and the related table creation script:</p>
  
  <pre class="lang-sql prettyprint-override"><code>create table T_Companies
  (
      CompanyId            int identity (10) identity
          primary key nonclustered,
      CompanyName          varchar(1024) not null,
      CompanyTypeId        int           not null
          constraint FK_Company_CompanyType
              references T_CompanyTypes,
      CountryId            int           not null
          constraint FK_Company_Country
              references T_Countries,
      Active               bit           not null,
      AccountId            int,
      CallbackSalt         varchar(1024),
      UserSpaceId          int,
      Login                varchar(1024),
      Pwd                  varchar(1024),
      StatusDate           datetime(23, 3),
      BankStatusCode       int           not null,
      PivotalAccount       varchar(255),
      IsTechnicalAccount   bit           not null,
      CompanyStreetAddress varchar(256),
      CompanyCity          varchar(256),
      CompanyZipCode       varchar(10),
      CompanyVatNumber     varchar(30),
      ContactFirstName     varchar(20),
      ContactLastName      varchar(20),
      IsKycIdSent          bit           not null
  )
  go
  </code></pre>
  
  <p>[EDIT 2]</p>
  
  <p>For the very same model, this (in the project file the nuget references) works</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.8" /&gt;
  &lt;PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.8" /&gt;
  </code></pre>
  
  <p>while those ones, no:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" /&gt;
  &lt;PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" /&gt;
  </code></pre>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" /&gt;
  &lt;PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.8" /&gt;
  </code></pre>