![](/img/trans.png)
[英]EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint
[英]SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint using EF code first C#
每次單擊“添加產品”時,都會出現以下錯誤。 我將非常感謝有關為什么我收到此錯誤的任何提示
SqlException: INSERT 語句與 FOREIGN KEY 約束“FK_Products_Categories_CategoryId”沖突。 沖突發生在數據庫“eCommerce”、表“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();
}
我的觀點: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>
我在 cshtml 中沒有看到您將在何處設置 CategoryId,這意味着它可能會設置為 0,這會導致違反外鍵約束。
在此行的控制器代碼中設置斷點:
_appDbContext.Products.Add(product);
並查看 CategoryId 是否為 0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.