title: Sql/c# Error: Cannot insert explicit value for identity column when IDENTITY_INSERT is set to off
body: <p>I have a web application which is giving me the following exception:</p>
  
  <blockquote>
    <p>InnerException    {"Cannot insert
    explicit value for identity column in
    table 'Cover' when IDENTITY_INSERT is
    set to OFF."} System.Exception
    {System.Data.SqlClient.SqlException}</p>
  </blockquote>
  
  <p>I'm trying to insert into a database which looks like the following:</p>
  
  <pre><code>dbo.File
  FileID            int  PK
  Title             nvarchar(50)
  ISBN              nvarchar(50)
  UploadDate        datetime
  UserName          nvarchar(50)
  
  dbo.Cover
  CoverID           int PK
  CoverFileContent  varbinary(max)
  CoverMimeType     nvarchar(50)
  CoverFileName     nvarchar(50)
  FileID            int FK
  
  dbo.Cover
  CoverID           int  PK
  CoverFileContent  varbinary(max)
  CoverMimeType     nvarchar(50)
  CoverFileName     nvarchar(50)
  FileID            int FK
  </code></pre>
  
  <p>In this database the File table has a one to many relationship with both the Cover, and the Pdf table. In my application, the user first enters a description of the file, and this updates the file table, then they upload a picture associated with the file which updates the Cover table, and then they upload a PDF file (I have not got this far yet). </p>
  
  <p>Right now, my MVC c# code looks like the following:</p>
  
  <p><strong>FileController</strong></p>
  
  <pre><code> //
      //GET: /File/CreateFile
      public ActionResult CreateFile()
      {
          File file = new File();
  
          return View(file);
      }
  
  //
  //POST: /File/CreateFile
  [HttpPost]
  public ActionResult CreateFile(FormCollection formvalues)
  {
      File file = new File();
  
      if (TryUpdateModel(file))
      {
          filerepository.AddFileData(file);
          filerepository.Save();
  
          return RedirectToAction("CreateCover", "Cover", new { id = file.FileID });
      }
  
      return View(file);
  }
  </code></pre>
  
  <p><strong>Cover Controller</strong></p>
  
  <pre><code>//
          //GET: /File/CreateCover
          public ActionResult CreateCover(int id)
          {
              Cover cover = new Cover();
              cover.FileID = id;
              return View(cover);
          }
  
          //
          //POST: /File/CreateCover
          [HttpPost]
          public ActionResult CreateCover(Cover cover)
          {
              cover.CoverMimeType = Request.Files["CoverUpload"].ContentType;
              Stream fileStream = Request.Files["CoverUpload"].InputStream;
              cover.CoverFileName = Path.GetFileName(Request.Files["CoverUpload"].FileName);
              int fileLength = Request.Files["CoverUpload"].ContentLength;
              cover.CoverFileContent = new byte[fileLength];
              fileStream.Read(cover.CoverFileContent, 0, fileLength);
              cover.FileID = int.Parse(Request.Form["FileID"]);
              filerepository.AddCoverData(cover);
  
              filerepository.Save();
  
              return View(cover);
              //return View("CreatePdf", "Pdf", new { id = cover.FileID });
          }
  </code></pre>
  
  <p>CreateCover View</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;SampleApp.Models.Cover&gt;" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt;
      CreateCover
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" 
  
  runat="server"&gt;
  
          &lt;h2&gt;CreateCover&lt;/h2&gt;
  
          &lt;% using (Html.BeginForm("CreateCover", "Cover", FormMethod.Post, new { enctype = "multipart/form-data" }))
             { %&gt;
             &lt;%: Html.HiddenFor(model =&gt; model.FileID) %&gt;
          &lt;asp:Label ID="Label2" runat="server" Text="Please Select your eBook Cover" /&gt;&lt;br /&gt;
          &lt;input type="file" name="CoverUpload" /&gt;&lt;br /&gt;
          &lt;input type="submit" name="submit" id="Submit" value="Upload" /&gt;
  
          &lt;% } %&gt;
  
          &lt;div&gt;
              &lt;%: Html.ActionLink("Back to List", "Index") %&gt;
          &lt;/div&gt;
  
      &lt;/asp:Content&gt;
  </code></pre>
  
  <p>I was able to upload a cover once, however ever since then I've been getting this error. I noticed that in my table I had forgotten to set the PK column as IDENTITY (1, 1), however I changed this and am still getting the error. </p>
  
  <p>I also don't know if it's worth mentioning, but in my database for the one image I was able to upload, the binary content in the database is pretty much "0x00000...."...This seems wrong? :)</p>
  
  <p>Any help or advice would be appreciated, thanks!</p>