SqlException:INSERT 语句与使用 EF 代码优先 C# 的 FOREIGN KEY 约束冲突

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint using EF code first C#

每次单击 "Add Product" 时都会出现以下错误。我将非常感谢关于为什么我会收到此错误的任何提示

SqlException:INSERT 语句与 FOREIGN KEY 约束冲突 "FK_Products_Categories_CategoryId"。冲突发生在数据库 "eCommerce"、table "dbo.Categories"、列 'CategoryId' 中。声明已终止。

主视图:

                <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; }
    }

我的模特: Product.cs 和 Category.cs 型号:

  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; }
    }

我的控制器: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();
        }

我的看法: 添加Product.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>

我在 cshtml 中看不到您要设置 CategoryId 的位置,这意味着它可能会设置为 0,这会导致违反外键约束。

在此行的控制器代码中设置断点:

_appDbContext.Products.Add(product); 

并查看 CategoryId 是否为 0。