I get the error below each time I click on the "Add Product". I would be so thankful for any tips on why I get this error
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Products_Categories_CategoryId". The conflict occurred in database "eCommerce", table "dbo.Categories", column 'CategoryId'. The statement has been terminated.
Home view:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-controller="Product" asp-action="AddProduct">Add product</a></li>
enter code here
</ul>
</div>
AppDbContext.cs
public class AppDbContext : IdentityDbContext<IdentityUser>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
My models: Product.cs and Category.cs models:
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public decimal Price { get; set; }
public string ImageUrl { get; set; }
public string ImageThumbnailUrl { get; set; }
public bool IsProductOfTheWeek { get; set; }
public bool InStock { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public List<Product> Products { get; set; }
}
My controller: Productontroller.cs
[HttpPost]
public IActionResult AddProduct(Product product)
{
if (ModelState.IsValid)
{
_appDbContext.Products.Add(product);
_appDbContext.SaveChanges();
return RedirectToAction("SeedComplete");
}
return View(product);
}
public IActionResult SeedComplete()
{
ViewBag.SeedCompleteMessage = "Thanks for adding the product!";
return View();
}
My view: AddProduct.cshtml
@model Product
<form asp-action="AddProduct" method="post" class="form-horizontal" role="form">
<h4>You're just one step away.</h4>
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Price" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ShortDescription" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="ShortDescription" class="form-control" />
<span asp-validation-for="ShortDescription" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="LongDescription" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="LongDescription" class="form-control" />
<span asp-validation-for="LongDescription" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ImageUrl" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="ImageUrl" class="form-control" />
<span asp-validation-for="ImageUrl" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ImageThumbnailUrl" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="ImageThumbnailUrl" class="form-control" />
<span asp-validation-for="ImageThumbnailUrl" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="IsProductOfTheWeek" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="IsProductOfTheWeek" class="form-control" />
<span asp-validation-for="IsProductOfTheWeek" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="InStock" class="col-md-2 control-label"></label>
<div class="col-md-5">
<input asp-for="InStock" class="form-control" />
<span asp-validation-for="InStock" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-5">
<input id="btn" type="submit" class="btn btn-primary" value="Complete the seed" />
</div>
</div>
</form>
SeedComplete.cshtml:
<h1>@ViewBag.SeedCompleteMessage </h1>