Sql/c# Error: Cannot insert explicit value for identity column when IDENTITY_INSERT is set to off
I have a web application which is giving me the following exception:
InnerException {"Cannot insert explicit value for identity column in table 'Cover' when IDENTITY_INSERT is set to OFF."} System.Exception {System.Data.SqlClient.SqlException}
I'm trying to insert into a database which looks like the following:
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
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).
Right now, my MVC c# code looks like the following:
FileController
//
//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);
}
Cover Controller
//
//GET: /File/CreateCover
public ActionResult CreateCover(int id)
{
Cover cover = new Cover();
cover.FileID = id;
return View(cover);
}
//
//POST: /File/CreateCover
[HttpPost]
public Act开发者_Python百科ionResult 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 });
}
CreateCover View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SampleApp.Models.Cover>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
CreateCover
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"
runat="server">
<h2>CreateCover</h2>
<% using (Html.BeginForm("CreateCover", "Cover", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<%: Html.HiddenFor(model => model.FileID) %>
<asp:Label ID="Label2" runat="server" Text="Please Select your eBook Cover" /><br />
<input type="file" name="CoverUpload" /><br />
<input type="submit" name="submit" id="Submit" value="Upload" />
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
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.
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? :)
Any help or advice would be appreciated, thanks!
Solved this issue for anyone else who is having the same problem. Simply refreshed the model in Visual Studio and it seems to have done the trick.
精彩评论