title: How to set optional field in code first approach in ASP.NET 5 and EF7
body: <p>I am working on a project and I would like to set some properties to optional in code first approach. In my class file I have added <code>[Required]</code> to properties I would set to not null in SQL and properties that do not have <code>[Required]</code> should set to allow null in SQL, but for some properties that contains ID in it, it sets to not null in SQL. Below is my sample class that contains properties that should be used when generating tables in the database. I would like to set <code>EnvironmentLastDeployedId</code> property to optional when it creates new database in MSSQL.</p>
  
  <pre><code>public class Version
  {
      public int Id { get; set; }
  
      [Required]
      public string PackageName { get; set; }
      public string VersionName { get; set; }
      public string OriginalPackageName { get; set; }
  
      [Required]
      public string CommitID { get; set; }
      public string CommitMessage { get; set; }
      public int ApplicationId { get; set; }
      public Application Application { get; set; }
      public string CreatedBy { get; set; }
      public int EnvironmentLastDeployedId { get; set; }
      public Environment EnvironmentLastDeployed { get; set; }
      public int StatusId { get; set; }
      public Status Status { get; set; }
      public DateTime CreatedOn { get; set; }
      public DateTime ModifiedOn { get; set; }
  }
  </code></pre>
  
  <p>In my Context class file, I tried to use Fluent API, but I only get <code>IsRequired</code> method, but not <code>IsOptional</code> or something that allows me to set table column to allow null in database.</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Version&gt;()
              .Property(p =&gt; p.EnvironmentLastDeployedId).IsRequired();
      }
  </code></pre>