I have already implemented the auto-increment for database, the identity_insert is set to OFF.
However, when I tried to insert data into the other columns, I get the "Cannot insert explicit value for identity column in table 'Bookmarks' when IDENTITY_INSERT is set to OFF." error. The code below is the table definition:
CREATE TABLE [dbo].[Bookmarks] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[carparkId] INT NULL,
[date] DATETIME NULL,
[username] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
FOREIGN KEY ([carparkId]) REFERENCES [dbo].[Carparks] ([id])
);
The code below shows the inserting of the data and the error occurs on db.SaveChanges().
public void Insert(Bookmark obj)
{
data.Add(obj);
db.SaveChanges();
}
This is the data i am trying to pass in:
public ActionResult Create([Bind(Include = "carparkId,date,username")] Bookmark bookmark)
{
if (ModelState.IsValid)
{
bookmarkGateway.Insert(bookmark);
return RedirectToAction("Index");
}
Any idea how to solve this error without turning on the identity_insert ? Your help is much appreciated thanks.