title: MVC - Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF
body: <p>When submitting my create form for my model, I get the error:</p>
  
  <pre><code>{"Cannot insert explicit value for identity column in table 't_bcim_project_clearance' when IDENTITY_INSERT is set to OFF."}
  </code></pre>
  
  <p>I can see this question has been asked a lot but I've made many changes to my code with no luck at all. I'm sorry in advance that it's so much code, I usually get people complaining in the comments that I don't post enough code so I tried to include everything that could possibly be relevant this time. </p>
  
  <p>The method in my controller that throws this error is:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult MaintenanceForm([Bind(Exclude = "project_clearance_id")]t_bcim_project_clearance project)
  {
      if (ModelState.IsValid)
      {
          if (entities.t_bcim_project_clearance.Any(x =&gt; x.project_clearance_id == project.project_clearance_id))
          {
              entities.Entry(project).State = EntityState.Modified;
          }
          else
          {
              entities.t_bcim_project_clearance.Add(project);
          }
          entities.SaveChanges();
          return RedirectToAction("Maintenance", new { SubmissionNumber = project.Submission_Number, Mode = "Edit" });
      }
      var model = new ProjectViewModel()
      {
          Project = entities.t_bcim_project_clearance.Find(project.project_clearance_id),
          States = states,
          Branches = branches,
          Divisions = divisions,
          ProjectTypes = project_types,
          Statuses = statuses,
          ReadOnly = false,
          Mode = "Edit",
          Cleared = false
      };
  
      return RedirectToAction("Maintenance", new { SubmissionNumber = project.Submission_Number, Mode = "Edit" });
  }
  </code></pre>
  
  <p>at the line <code>entities.SaveChanges();</code>. <code>entities</code> is my Context class for connecting to the SQL database.</p>
  
  <p>Here's the corresponding GET method:</p>
  
  <pre><code>[HttpGet]
  public ActionResult MaintenanceForm(string SubmissionNumber, string Mode, int? id)
  {
      if (!Mode.Equals("Edit", StringComparison.InvariantCultureIgnoreCase) &amp;&amp; !Mode.Equals("View", StringComparison.InvariantCultureIgnoreCase))
      {
          throw new HttpException(400, "Mode must be either \"Edit\" or \"View\"");
      }
  
      var model = new ProjectViewModel()
      {
          Project = id.HasValue ? entities.t_bcim_project_clearance.Find(id) : (String.IsNullOrEmpty(SubmissionNumber) ? null : new t_bcim_project_clearance() { Submission_Number = Convert.ToDouble(SubmissionNumber) }),
          Statuses = statuses,
          Divisions = divisions,
          Branches = branches,
          ProjectTypes = project_types,
          States = states,
          Projects = entities.t_bcim_project_clearance.Where(s =&gt; s.Submission_Number.ToString().Equals(SubmissionNumber)),
          SubmissionNumber = SubmissionNumber,
          ReadOnly = (Mode == "View"),
          Mode = Mode,
          Cleared = false
      };
  
      return PartialView("_MaintenanceForm", model);
  }
  </code></pre>
  
  <p>My model is a DBmodel. This is the offending property in that model:</p>
  
  <pre><code>namespace ProjectClearanceApp.Models
  {
      using System;
      using System.Collections.Generic;
  
      public partial class t_bcim_project_clearance
      {
          // A lot of other properties
          public int project_clearance_id { get; set; }
      }
  }
  </code></pre>
  
  <p>and I created a partial class to add DataAnnotations, which is here:</p>
  
  <pre><code>namespace ProjectClearanceApp.Models
  {
      [MetadataType(typeof(t_bcim_project_clearanceMetaData))]
      public partial class t_bcim_project_clearance
      {
      }
  
      public class t_bcim_project_clearanceMetaData
      {
          // a lot of other properties
  
          [Key]
          [Required]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int project_clearance_id { get; set; }
      }
  }
  </code></pre>
  
  <p>My <code>project_clearance_id</code> is set as the primary key for my table, with the Identity Increment = 1.</p>
  
  <p><strong>WHAT I'VE TRIED SO FAR</strong>:</p>
  
  <ul>
  <li>Originally my controller was generating new projects with a precalculated ID, so obviously I removed that</li>
  <li>I added all of the DataAnnotations to the ID property in that partial class</li>
  <li>I used to have <code>Html.HiddenFor(model =&gt; model.project_clearance_id)</code> at the top of my form but I removed that</li>
  <li>I added the <code>[Bind(Exclude = "project_clearance_id")]</code> to the arguments for the POST method</li>
  </ul>
  
  <p>Nothing seems to be working. If anyone sees anything I could change that would be appreciated.</p>
  
  <p><strong>EDIT</strong>: I went into the .edmx diagram for my model, right clicked on the <code>project_clearance_id</code>, opened properties, and changed <code>StoreGeneratedPattern</code> to <code>Identity</code>. Now the code works fine when I'm trying to create a new entry, but when I try to edit an existing entry, it creates a new entry with the modified fields and the original entry still exists as it was before.</p>