I am trying to save some data into my MenuItems table but I'm prevented by the error
SqlException: Cannot insert explicit value for identity column in table 'MenuCategories' when IDENTITY_INSERT is set to OFF
I am just trying to set the foreign key using the category id I have, I am not quite sure why I am getting the above error.
This is my MenuItem model:
public class MenuItemViewModel
{
[Key]
[Column("MenuItem_Id")]
public int MenuItemId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public decimal Price { get; set; }
[Required]
[Column("MenuCategory_Id")]
public MenuCategoryViewModel MenuCategory { get; set; }
}
This is my MenuCategory model:
public class MenuCategoryViewModel
{
[Key]
[Column("MenuCategory_Id")]
public int MenuCategoryId { get; set; }
[Required]
public string Name { get; set; }
}
I'm trying to assign it the id of an existing category, I do not want it to create a new category.
MenuItemIdunless you setIdentity_Insertto off. I am not an expert on Entity Framework Core, but have you tried to pass the object without theMenuItemIdpopulated? Why do you need to populate theMenuItemIdyourself? - Kristianne Nerona