[英]Delete record that has a foreign key
I'm having issues with my DELETE, If a record has a FK the record won't delete, I have tried to implement a Soft delete approach by adding a bit IsDeleted column and setting the rules.. but that also is in vain.. here is my code 我的DELETE出现问题,如果一条记录包含FK,则该记录不会被删除,我试图通过添加一点IsDeleted列并设置规则来实现软删除方法。但这也是徒劳的。这是我的代码
// DELETE: api/ProductCategory/5
[Authorize]
[ResponseType(typeof(Product_Category))]
public async Task<IHttpActionResult> Delete_Product_Category(int id)
{
Product_Category Product_Category = await db.Product_Category.FindAsync(id);
if (CRM_Product_Category == null)
{
return NotFound();
}
db.Product_Category.Remove(Product_Category);
await db.SaveChangesAsync();
return Ok(Product_Category);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool Product_CategoryExists(int id)
{
return db.Product_Category.Count(e => e.ProductCategoryID == id) > 0;
}
It's normal, you are trying to delete a category , but how the DB is supposed to handle products linked to that specific category ? 这是正常的,您要删除一个类别 ,但DB应该如何处理产品链接到特定类别?
You have 2 choices here : 您在这里有2个选择:
1 - Checking if you have products linked to the category and delete them or change the category.
1-检查是否有产品链接到类别并删除它们或更改类别。
2 - Deleting on cascade.
2-级联删除。 (It will delete your linked records on cascade BE CAREFULL)
(它将删除级联上的链接记录,请谨慎操作)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.