title: EF Core - column does not allow nulls. INSERT fails
body: <blockquote>
    <p>Cannot insert the value NULL into column 'AttributeId', table
    'TableName'; column does not allow nulls. INSERT fails.</p>
  </blockquote>
  
  <p>The object that I'm passing to EF Core to insert a new record in the database
  looks like this</p>
  
  <pre><code>public int Id { get; set; }
  public string Value { get; set; }
  public int AttributeId { get; set; }
  public int CustomAttributeId { get; set; }
  </code></pre>
  
  <p>I get a new instance of the object and pass it to my Db Context.
  But it's complaining that AttributeId cannot be null which makes sense.
  But the property's data type is Int. I'm not too sure why EF Core is interpreting the value as null. I went as far as hardcoding a zero but it still complains.</p>
  
  <p>Has anyone ever encountered a similar issue? The error is thrown in UpsertAttribute()</p>
  
  <p><strong>Update</strong></p>
  
  <p>Core Layer</p>
  
  <pre><code>Db.LineAttribute lineAttribute = new Db.LineAttribute()
  {
      Value = attribute.Value ?? "",
      CustomAttributeId = attribute.Id,
  };
  
  if (attribute.Values.Count &gt; 0 &amp;&amp; String.IsNullOrEmpty(attribute.Value))
  {
      if (attribute.Values.Where(x =&gt; x.IsDefault == true &amp;&amp; x.Id == -1).Count() == 1)
      {
          // Set default value
          lineAttribute.Value = attribute
             .Values
             .FirstOrDefault(x =&gt; x.Id == -1)
             .InternalValue;
      }
      else
      {
          // Set user selected default value
          lineAttribute.Value = attribute
              .Values
              .FirstOrDefault(x =&gt; x.IsDefault = true)
              .InternalValue;
      }
  }
  
  lineAttribute = await _dataLayer.UpsertAttribute(lineAttribute);
  </code></pre>
  
  <p>Data Layer</p>
  
  <pre><code>private readonly AppContext _db;
  
  ----------------
  
  private async Task&lt;lineAttribute&gt; UpsertAttribute(lineAttribute attr)
  {
      _db.Entry(attr).State = attr.id &lt;= 0 ? EntityState.Added : EntityState.Modified;
      await _db.SaveChanges();
  
      return attr;
  }
  </code></pre>