I'm trying to add a product to a category using this code
public void AddProductToCategory()
{
if (categoryExist)
{
var product = FindProduct(productArticleNumber);
var productExist = product != null;
if (productExist)
{
AddProduct(product);
}
}
im getiing an error saying:
Cannot insert explicit value for identity column in table ‘MyTable’ when IDENTITY_INSERT is set to OFF.
Here is my tables:
CREATE TABLE Product (
Id INT IDENTITY PRIMARY KEY,
Name NVARCHAR(50) NOT NULL,
ArticleNumber NVARCHAR(50) NOT NULL,
DESCRIPTION NVARCHAR (500),
Price INT,
UNIQUE (Articlenumber),
UNIQUE (Name)
)
CREATE TABLE Categories(
Id INT IDENTITY PRIMARY KEY,
Name NVARCHAR (10),
ProductId INT,
FOREIGN KEY (ProductId)
REFERENCES Product (Id)
)
Here are the
AddProduct
Code:
private static void AddProduct(Product product)
{
context.Product.Add(product);
context.SaveChanges();
}
I tried to set identity_Insert to On like that:
SET IDENTITY_INSERT Product ON
But im still getting the same error