title: EntityFramework Core 2 Default Values
body: <p>I'm trying to use default values on a couple of my EF models and I'm noticing that either I'm misunderstanding the <code>HasDefaultValue</code> behaviour or its not working as it should.</p>
  
  <p>I have the following table and backing model</p>
  
  <pre><code>CREATE TABLE [dbo].[packages]
  (
     [Scnumber] BIGINT NOT NULL, 
     [PU] INT NOT NULL,
     [State] SMALLINT NOT NULL DEFAULT 0, 
     [Status] [smallint] NOT NULL, 
     [Created] DATETIME NOT NULL DEFAULT GETDATE(), 
     CONSTRAINT [PK_packages] PRIMARY KEY CLUSTERED ([Scnumber] ASC) ON [PRIMARY], 
  )
  
  [Table("packages", Schema = "dbo")]
  public class Package
  {
     [Key]
     [Column(TypeName = "bigint")]
     public long Scnumber { get; set; }
  
     [Column]
     [Required]
     public int PU { get; set; }
  
     [Column(TypeName = "smallint")]
     [Required]
     public PackageStatus Status { get; set; }
  
     [Column(TypeName = "tinyint")]
     public RecordState State { get; set; }
  
     [Column]
     public DateTime? Created { get; set; }    
  }
  </code></pre>
  
  <p>In order to apply default values I've also got the following in my <code>OnModelCreating()</code></p>
  
  <pre><code>modelBuilder.Entity&lt;Package&gt;(entity =&gt;
  {
     entity.Property(r =&gt; r.State)
        .HasDefaultValue(RecordState.Active);
  
     entity.Property(r =&gt; r.Created)
        .HasDefaultValue(DateTime.Now);
  });
  </code></pre>
  
  <p>When I attempt to save a new object I get the following exception: </p>
  
  <blockquote>
    <p>SqlException: Cannot insert the value NULL into column 'State', table
    'app.dbo.packages'; column does not allow nulls.
    INSERT fails.</p>
  </blockquote>
  
  <p>This is confusing as if I inspect the object before I save it, it has a default value, but yet I get this exception. I understand the easiest solution would be to simply make sure I set the value of state when I create the object, but this sort of defeats the purpose of <code>HasDefaultValue</code>.</p>
  
  <p>-- edit -- </p>
  
  <p>When I try to add a new object I do the following</p>
  
  <pre><code>var package = new Package { Scnumber = 0123456718, PU = 1001 };
  
  _context.Packages.Add(region);
  _context.SaveChanges();
  </code></pre>
  
  <p>Nothing about this is out of the norm but fails, but if I run the following it works</p>
  
  <pre><code>var package = new Package {Scnumber = 0123456718, PU = 1001, Status = PackageStatus.Rejected, State = RecordState.Staging, Created = DateTime.Now };
  
  _context.Packages.Add(region);
  _context.SaveChanges();
  
  public enum PackageStatus : short
  {
     New,
     PendingValidation,
     CheckedOut,
     Approved,
     Rejected,
     PendingRender,
     Rendered,
     RenderFailed,
     PendingPrint,
     Printed,
     PrintFailed,
     Cancelled,
     PendingDownload,
     Downloaded,
     DownloadError,
     SystemUpdated, 
  }
  
  public enum RecordState : byte
  {
      Active,
      Deleted,
      Staging,
  }
  </code></pre>