title: SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF
body: <p>I am creating a website for expense tracking and when I send a new POST request calling <code>AddNew</code> method to add a new Entry I get this error: </p>
  
  <blockquote>
    <p>SqlException: Cannot insert explicit value for identity column in table 'Accounts' when IDENTITY_INSERT is set to OFF. Cannot insert explicit value for identity column in table 'Categories' when IDENTITY_INSERT is set to OFF.</p>
  </blockquote>
  
  <pre><code>public class Entry
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      public string Description { get; set; }
      public decimal MoneyAmount { get; set; }
      public virtual Account Account { get; set; }
      public virtual Category Category { get; set; }
      public string CreatedTimestamp { get; set; }
  }
  
  public class Account
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual User User { get; set; }
      //public byte[] BankApi { get; set; }
      public decimal MoneyAmount { get; set; }
      public string CreatedTimestamp { get; set; }
      public virtual ICollection&lt;Entry&gt; Entries { get; set; }
  }
  
  public class Category
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual EntryType EntryType { get; set; }
      public virtual ICollection&lt;Entry&gt; Entries { get; set; }
  }
  
  public interface IEntryService
  {
      void AddNew(Entry entry);
  }
  
  public class EntryService : IEntryService
  {
      private DataContext _context;
  
      public EntryService(DataContext context)
      {
          _context = context;
      }
  
      public void AddNew(Entry entry)
      {
          entry.CreatedTimestamp = DateTime.Now.ToString();
  
          _context.Entries.Add(entry);
          _context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>Here is my postman request:</p>
  
  <p><img src="https://i.stack.imgur.com/D3zsq.png" alt="Postman request"></p>
  
  <p>Account with ID == 1, and Category with ID == 1 both exist already in the respective databases.</p>
  
  <p>Also, when I am inserting values to Account database, whose method looks exactly the same as <code>AddNew</code> for entries, it works without a problem.</p>
  
  <p>Does anyone know what is this about?</p>