ef核心一对多关系引发异常无法添加或更新子行 [英] ef core one to many relationship throw exception Cannot add or update a child row
问题描述
我的商店和产品实体之间存在一对多的关系,请查看我的模型
My Shop and Product entities have a one to many relationship, please see my models
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Tag { get; set; }
public string Brand { get; set; }
public string Place { get; set; }
public int ShopID { get; set; }
public Shop Shop { get; set; }
}
public class Shop
{
public int ID { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public string Number { get; set; }
public ICollection<Product> Products { get; set; }
}
public class ShopContext : DbContext
{
public ShopContext(DbContextOptions<ShopContext> options) : base(options)
{
}
public DbSet<Shop> Shops { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Shop>().ToTable("shop");
modelBuilder.Entity<Product>().ToTable("product");
}
}
public class CreateModel : PageModel
{
private readonly ShopContext _context;
public CreateModel(ShopContext context)
{
_context = context;
}
public IActionResult OnGet()
{
ViewData["Shop"] = new SelectList(_context.Shops, "ID", "Name");
return Page();
}
[BindProperty]
public Product Product { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Products.Add(Product);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
创建"页面中的商店是一个下拉列表,当我将新产品发布到处理程序时,我可以看到该产品.ShopID是Shop表中存在的ID,而product.Shop.ID与product.ShopID.但是当我调用saveChangesAsync()时,它将引发异常:
The shop in create page is a dropdown list, when I post the new product to the handler, I can see the product.ShopID is an ID which exist in Shop table, and the product.Shop.ID as same as the product.ShopID. But when I call saveChangesAsync(), it throws an exception:
MySqlException:无法添加或更新子行:外键约束失败(shopdb
.product
,CONSTRAINT FK_product_shop_ShopID
外键(ShopID
))参考shop
(ID
)在删除级联上)
MySqlException: Cannot add or update a child row: a foreign key constraint fails (shopdb
.product
, CONSTRAINT FK_product_shop_ShopID
FOREIGN KEY (ShopID
) REFERENCES shop
(ID
) ON DELETE CASCADE)
我试图在调用saveChangesAsync方法之前将product.Shop设置为null,但是仍然无法正常工作,任何人都可以帮忙吗?
I tried to set product.Shop to null before call saveChangesAsync method, but still doesn't work, anyone can help?
ASP.NET Core 2.0 Razor Pages应用程序, Nuget:Pomelo.EntityFrameworkCore.MySql v2.0.1
ASP.NET Core 2.0 Razor Pages Application, Nuget: Pomelo.EntityFrameworkCore.MySql v2.0.1
推荐答案
在Product
表中将ShopID
声明为可为空,您正在执行product.Shop = null;
,因此它将尝试在其中插入ShopID
值null
本专栏.由于您在Product
表中的ShopID
列不可为空,这就是其给出错误的原因.因为Shop
表中没有可为空的Id
.因此,其给出的外键约束失败例外.因此,您要做的就是使用可空的外键,例如public int? ShopID
.
Declare ShopID
as nullable in the Product
table, you are doing product.Shop = null;
, so it tries to insert ShopID
value null
in this column. Since you column ShopID
in Product
table is not nullable, that is why its giving error. Because there is no Id
in Shop
table which is nullable. So its giving foreign key constraint fails exception. So all you have to do is to make use the nullable foreign key like public int? ShopID
.
这篇关于ef核心一对多关系引发异常无法添加或更新子行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!