簡體   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