繁体   English   中英

删除具有外键的记录

[英]Delete record that has a foreign key

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

这是正常的,您要删除一个类别 ,但DB应该如何处理产品链接到特定类别?

您在这里有2个选择:

1-检查是否有产品链接到类别并删除它们或更改类别。

2-级联删除。 (它将删除级联上的链接记录,请谨慎操作)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM