gpt4 book ai didi

mysql - Entity Framework 5 : Cascade Delete One to Many

转载 作者:行者123 更新时间:2023-11-29 20:48:34 25 4
gpt4 key购买 nike

我想知道当我想删除我的客户时如何删除我的所有银行支票。

这是我的数据库

我做了这样的事情:

ConEntities context = new ConEntities();
context.Customer.Attach(selectedCustomer);
context.Customer.Remove(selectedCustomer);
context.SaveChanges();
context.Dispose();

但是我有这个错误:

Cannot delete or update a parent row: a foreign key constraint fails (``.BankCheck, CONSTRAINT fk_1 FOREIGN KEY (idCustomer) REFERENCES Customer (id))"

我将 Cascade 放在 OnDelete End1 上

最佳答案

已修复 this :

using (var context = new ConEntities())
{
var parent = context.Customer.Include(p => p.Check).SingleOrDefault(s => s.id == selectedCustomer.id);

if (parent.Check != null && parent.Check.Count > 0)
{
foreach (var child in parent.Check.ToList())
{
context.Check.Remove(child);
}
}

context.Customer.Attach(parent);
context.Customer.Remove(parent);
context.SaveChanges();
}

关于mysql - Entity Framework 5 : Cascade Delete One to Many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38242538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com