title: ASP.NET C# Class/Object with default values and database NOT NULL columns
body: <p>I am using SQL server for database while developing in ASP.NET . In my database table, most of the columns are NOT NULL and have a set default value. I am also using Entity Framework. </p>
  
  <p>My Model(class) currently looks like this:</p>
  
  <pre><code>public partial class INV
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
      public INV()
      {
          this.INVAttachments = new HashSet&lt;INVAttachment&gt;();
      }
  
      public int ID { get; set; }
      public string SUPPROD { get; set; }
      public string TYPE1 { get; set; }
      public int MAXQTY { get; set; }
      public decimal ROP { get; set; }
      public int ROQ { get; set; }
      public decimal SSL { get; set; }
      public Nullable&lt;System.DateTime&gt; SYSDATE { get; set; }
      public Nullable&lt;System.DateTime&gt; MAINT_DATE { get; set; }
  
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public ICollection&lt;INVAttachment&gt; INVAttachments { get; set; }
  } 
  </code></pre>
  
  <p>The user submits a form, and inside the form, most of fields are actually optional. But in the database, the columns are not null. </p>
  
  <p>When I POST the form over to the server side, the INV object representation has null for the properties of the INV object. Below is a WebAPI function to handle the POST and save the INV object in the database. </p>
  
  <pre><code>    [ResponseType(typeof(string))]
      public async Task&lt;string&gt; PostINV(INV iNV)
      {
          try
          {
              if (!ModelState.IsValid)
              {
                  //return BadRequest(ModelState);
                  return "badRequest";
              }
  
              db.INVs.Add(iNV);
              await db.SaveChangesAsync();
  
              //codes written here
  
  
          }catch (Exception ex)
          {
              return "";
          }
      }
  </code></pre>
  
  <p>The above function is now returning system.data.entity.validation.dbvalidationerror for the INV entity because the properties for INV is required, but the object that I POST over contain NULL values. </p>
  
  <p>My question is it possible to make sure the database columns remain NOT NULL but at the same time, I don't want my Model or Class to use Nullable types? Is it a matter of using a constructor for my class to set default values ? </p>