title: Entity framework core: Cannot insert explicit value for identity column in table 'Relation' when IDENTITY_INSERT is set to OFF
body: <p>I'm bulding an application and when I want to insert a form into my form table I get the following error: </p>
  
  <blockquote>
    <p>Cannot insert explicit value for identity column in table 'Relation'
    when IDENTITY_INSERT is set to OFF.</p>
  </blockquote>
  
  <p>These are my models:</p>
  
  <p>Form model:</p>
  
  <pre><code>[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      [ForeignKey("FormType")]
      public int? TypeId { get; set; }
      public virtual FormType Type { get; set; }
  
      [ForeignKey("FormStatusType")]
      public int? StatusTypeId { get; set; }
      public virtual FormStatusType StatusTknype { get; set; }
  
      [ForeignKey("Relation")]
      public int? SupplierId { get; set; }
      public virtual Relation Supplier { get; set; }
  
      [ForeignKey("Relation")]
      public int? CustomerId { get; set; }
      public virtual Relation Customer { get; set; }
  
      public String SupplierReference { get; set; }
      public Guid ApiId { get; set; }
      public DateTime DueDate { get; set; }
      public FormFile FormFiles { get; set; }
      public String FormName { get; set; }
      public DateTime UploadDate { get; set; }
  </code></pre>
  
  <p>Relation model:</p>
  
  <pre><code>[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      [ForeignKey("FormType")]
      public int? TypeId { get; set; }
      public virtual FormType Type { get; set; }
  
      [ForeignKey("FormStatusType")]
      public int? StatusTypeId { get; set; }
      public virtual FormStatusType StatusTknype { get; set; }
  
      [ForeignKey("Relation")]
      public int? SupplierId { get; set; }
      public virtual Relation Supplier { get; set; }
  
      [ForeignKey("Relation")]
      public int? CustomerId { get; set; }
      public virtual Relation Customer { get; set; }
  
      public String SupplierReference { get; set; }
      public Guid ApiId { get; set; }
      public DateTime DueDate { get; set; }
      public FormFile FormFiles { get; set; }
      public String FormName { get; set; }
      public DateTime UploadDate { get; set; }
  </code></pre>
  
  <p>My context looks like this:</p>
  
  <pre><code>public class DataContext: DbContext
  {
      public DataContext(DbContextOptions&lt;DataContext&gt; options): base(options)
      {
  
      }
  
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          base.OnConfiguring(optionsBuilder);
          optionsBuilder.UseSqlServer();
      }
  
      public DbSet&lt;Relation&gt; Relation { get; set; }
      public DbSet&lt;Setting&gt; Settings { get; set; }
      public DbSet&lt;Notification&gt; Notification { get; set; }
      public DbSet&lt;FormStatusType&gt; FormStatusType { get; set; }
      public DbSet&lt;File&gt; File { get; set; }
      public DbSet&lt;FormFile&gt; FormFile { get; set; }
      public DbSet&lt;FormType&gt; FormType { get; set; }
      public DbSet&lt;Form&gt; Form { get; set; }
      public DbSet&lt;User&gt; User { get; set; }
      public DbSet&lt;RelationUser&gt; RelationUser { get; set; }
      public DbSet&lt;SupplierCustomer&gt; SupplierCustomer { get; set; }
  
  }
  </code></pre>
  
  <p>The method I use to add a form looks like this:</p>
  
  <pre><code> public async Task&lt;Form&gt; AddForm(Form form, int currentUserId)
      {
          try
          {
              if (form != null)
              {
                  //huidige gebruiker als supplier aanduiden
                  Relation r = await GetCurrentUser(currentUserId);
                  form.Supplier = r;
                  form.SupplierId = r.Id;
  
                  //form aan de db toevoegen
                  _datacontext.Form.Add(form);
                  _datacontext.SaveChanges();
  
                  return form;
              }
              else
              {
                  return null;
              }
          }
          catch (Exception e)
          {
              LogError(e);
              return null;
          }
      }
  </code></pre>
  
  <p>The get current user method</p>
  
  <pre><code> private async Task&lt;Relation&gt; GetCurrentUser(int currentUserId)
      {
          var relation = from r in _datacontext.RelationUser
                         where r.UserId == currentUserId
                         select r.Relation;
          return await relation.FirstOrDefaultAsync();
      }
  </code></pre>
  
  <p>This is where I call the AddForm method:</p>
  
  <pre><code> [HttpPost]
      [Route("addform")]
      [Authorize]
      // api/form/addform
      public async Task&lt;IActionResult&gt; AddForm([FromBody] Form form)
      {
          if (ModelState.IsValid)
          {
              Form f = await _formRepository.AddForm(form, GetUserIdFromToken());
  
              if(f != null)
              {
                  QueueObject qo = new QueueObject()
                  {
                      ActionTypeId = 1,
                      FormId = f.Id
                  };
                  await new QueueHandler().SendMessageToQueue(qo);
              }
  
              return Ok(f);
          }
          else
          {
              return NotFound("model is niet geldig");
          }
      }
  </code></pre>
  
  <p>I already searched but found nothing that solved the problem</p>