title: EF Code first : Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF
body: <p>Set the primary key to identity in database (SQL Server)</p>
  
  <p>When using EF to insert data I am getting error </p>
  
  <blockquote>
    <p>Cannot insert explicit value for identity column in table 'myTable'
    when IDENTITY_INSERT is set to OFF</p>
  </blockquote>
  
  <p>with this particular entity, though I am using the same approach with other entities to insert the data and getting no error with them.</p>
  
  <pre><code>   public class MyTable 
      {
      [Key/*,DatabaseGenerated(DatabaseGeneratedOption.Identity)*/]
      public int ID{get;set;}
      public string name {get;set;}
      public string type {get;set;}
      public string status {get;set;}
      public int country {get;set;}
      public DateTime start_date {get;set;}
      public DateTime due_date {get;set;}
      }`
  </code></pre>
  
  <p>Query</p>
  
  <pre><code>    MyTable check= new MyTable();
              check.name = checkName;
              check.type = "DB";
              check.status = "New";
              check.country = country;
              check.start_date = DateTime.Now;
              check.due_date = dueDate;
              db.myTables.Add(check);
              db.SaveChanges();
  </code></pre>
  
  <p><b>Updated </b>:</p>
  
  <p>After uncommenting the above line </p>
  
  <pre><code>(DatabaseGenerated(DatabaseGeneratedOption.Identity)) 
  </code></pre>
  
  <p>it's throwing an exception </p>
  
  <blockquote>
    <p>A dependent property in a ReferentialConstraint is mapped to a
    store-generated column. Column: 'ID'</p>
  </blockquote>