I have simple DataTable
CREATE TABLE [dbo].[Table] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
With one record, Name="Test" Id get's automatic 0.
When I am using basic create operation
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Table table)
{
if (ModelState.IsValid)
{
db.Table.Add(table);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(table);
}
I get error:
Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF.
What I am doing wrong? How to fix this error?
Table class
public partial class Table
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
}