0

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>
Muss Mesmari
  • 123
  • 10

1 Answers1

0

I don't see in the cshtml where you would be setting CategoryId, which means it is likely getting set to 0, which would cause a foreign key constraint violation.

Set a breakpoint in your controller code on this line:

_appDbContext.Products.Add(product); 

And see if the CategoryId is 0.

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
  • It was zero ad you said. I have added the CategoryId to the view and it is still zero – Muss Mesmari Mar 20 '20 at 14:39
  • Show the new view code? But really, you need to figure out why the categoryId is not being sent. Check the data you are sending via the browser--if you don't know how to do this, look at this question: https://stackoverflow.com/questions/15603561/how-can-i-debug-a-http-post-in-chrome. – Phil Sandler Mar 20 '20 at 16:07
  • Thanks for your help. The problem was in the view as you said. The categoryId is not being sent. – Muss Mesmari Apr 15 '20 at 17:27