简体   繁体   English

DELETE语句与REFERENCE约束“ FK__tbl8_update__HID__55F4C372”冲突

[英]The DELETE statement conflicted with the REFERENCE constraint “FK__tbl8_update__HID__55F4C372”

I have the following 2 tables 我有以下2张桌子

CREATE TABLE HOLIDAY_DATE_TABLE
(
HID INT IDENTITY PRIMARY KEY,
TABLE_NUMBER nchar(2) NOT NULL,
HOLIDAY_DATE nchar(8) NOT NULL,
FIELD_DESCRIPTION nVARchar(43) NULL,
);


CREATE TABLE tbl8_update_transactions
(
TID INT IDENTITY PRIMARY KEY,
TABLE_NUMBER nchar(2) NOT NULL,
HOLIDAY_DATE nchar(8) NOT NULL,
FIELD_DESCRIPTION nVARchar(43) NULL,
HID int,
FOREIGN KEY (HID) REFERENCES HOLIDAY_DATE_TABLE (HID)
);

When I perform a delete via my application view (Delete.cshtml), I get the following error: 通过应用程序视图(Delete.cshtml)执行删除时,出现以下错误:

"The DELETE statement conflicted with the REFERENCE constraint "FK__tbl8_update__HID__55F4C372". The conflict occurred in database "BillingUI", table "tbl8_update_transactions", column 'HID'. The statement has been terminated." “ DELETE语句与REFERENCE约束“ FK__tbl8_update__HID__55F4C372发生冲突。该冲突发生在数据库“ BillingUI”的表“ tbl8_update_transactions”的“ HID”列中。该语句已终止。”

Basically, when a delete is performed on a particular row from my view, I want it to delete the same record in both HOLIDAY_DATE_TABLE and tbl8_update_transactions. 基本上,当我从视图中对特定行执行删除时,我希望它删除HOLIDAY_DATE_TABLE和tbl8_update_transactions中的同一记录。 I read that I have to delete the child and then the parent. 我读到我必须先删除孩子然后再删除父母。 I tried changing the order in which the deletes take place, however when I do this it deletes the wrong record from tbl8_update_transactions because it's looking at its primary key for identification and not its foreign key. 我尝试更改删除的顺序,但是当我这样做时,它会从tbl8_update_transactions中删除错误的记录,因为它正在寻找其主键进行标识,而不是其外键。 My code is below, thanks. 我的代码在下面,谢谢。

 public ActionResult Delete(int id = 0)
    {
        HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
        //tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
        if (holiday_date_table == null)
        {
            return HttpNotFound();
        }
        return View(holiday_date_table);
    }

    //
    // POST: /Table8/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
        db.HOLIDAY_DATE_TABLE.Remove(holiday_date_table);
        db.SaveChanges();
        tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
        db.tbl8_update_transactions.Remove(tbl8_update_transaction);
        db.SaveChanges();
        return RedirectToAction("../Billing/HolidayDateTable");
    } 

Update: I think I wasn't clear enough. 更新:我想我还不够清楚。 I tried reversing them already, ie; 我已经尝试过扭转它们,即;

public ActionResult Delete(int id = 0)
    {
        HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
        //tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
        if (holiday_date_table == null)
        {
            return HttpNotFound();
        }
        return View(holiday_date_table);
    }

    //
    // POST: /Table8/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {          
        tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
        db.tbl8_update_transactions.Remove(tbl8_update_transaction);
        db.SaveChanges();
        HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
        db.HOLIDAY_DATE_TABLE.Remove(holiday_date_table);
        db.SaveChanges();
        return RedirectToAction("../Billing/HolidayDateTable");
    }

but it doesn't work, I receive the error "Value cannot be null. Parameter name: entity", pointing to db.tbl8_update_transactions.Remove(tbl8_update_transaction); 但它不起作用,我收到错误消息“值不能为空。参数名称:实体”,指向db.tbl8_update_transactions.Remove(tbl8_update_transaction);。

This is because (at least I think) that it's deleting from tbl8_update_transaction where it's unique identifier TID is equal to the value of HID in HOLIDAY_DATE_TABLE. 这是因为(至少我认为)它是从tbl8_update_transaction中删除的,它的唯一标识符TID等于HOLIDAY_DATE_TABLE中的HID值。 I need it to delete from tbl8_update_transaction using its foreign key value and am not sure how in C#. 我需要它使用其外键值从tbl8_update_transaction中删除,并且不确定如何在C#中使用它。

Update 2: I tried having it identify the row by the HID (foreign key) value in tbl8_update_transaction... 更新2:我试图让它通过tbl8_update_transaction中的HID(外键)值识别行...

 public ActionResult Delete(int id = 0, int HID = 0)
    {
        HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
        tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(HID);
        if (holiday_date_table == null)
        {
            return HttpNotFound();
        }
        return View(holiday_date_table);
    }

    //
    // POST: /Table8/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id, int HID)
    {          
        tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(HID);
        db.tbl8_update_transactions.Remove(tbl8_update_transaction);
        db.SaveChanges();
        HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
        db.HOLIDAY_DATE_TABLE.Remove(holiday_date_table);
        db.SaveChanges();
        return RedirectToAction("../Billing/HolidayDateTable");
    }

But get the following error: The parameters dictionary contains a null entry for parameter 'HID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult DeleteConfirmed(Int32, Int32)' in 'BillingApp.Controllers.Table8Controller'. 但是会出现以下错误:参数字典在'BillingApp.Controllers'中为方法'System.Web.Mvc.ActionResult DeleteConfirmed(Int32,Int32)'的非空类型'System.Int32'的参数'HID'包含空条目.Table8Controller'。 An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。 Parameter name: parameters 参数名称:参数

You need to flip your Remove methods around. 您需要翻转您的Remove方法。 You are deleting the parent first when there is still a child. 如果还有孩子,则要先删除父母。 You need to first delete the row from tbl8_update_transaction and then holiday_date_table. 您需要首先从tbl8_update_transaction中删除该行,然后从holiday_date_table中删除。

tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
db.tbl8_update_transactions.Remove(tbl8_update_transaction);
db.SaveChanges();
HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
db.HOLIDAY_DATE_TABLE.Remove(holiday_date_table);
db.SaveChanges();

You should delete all the objects where this ID is Foreign key. 您应该删除此ID为外键的所有对象。 They will be not valid if you delete this reference, because of that you have this exception. 如果您删除此引用,它们将无效,因为您有此异常。

Try using ON DELETE CASCADE on the child table's Foreign Key Constraint. 尝试在子表的Foreign Key约束上使用ON DELETE CASCADE

CREATE TABLE tbl8_update_transactions
(
TID INT IDENTITY PRIMARY KEY,
TABLE_NUMBER nchar(2) NOT NULL,
HOLIDAY_DATE nchar(8) NOT NULL,
FIELD_DESCRIPTION nVARchar(43) NULL,
HID int,
FOREIGN KEY (HID) REFERENCES HOLIDAY_DATE_TABLE (HID) ON DELETE CASCADE
);

The ON DELETE CASCADE implements a cascading delete on the parent table's rows which will delete the associated child table's rows when a parent table's row is deleted. ON DELETE CASCADE在父表的行上实现级联删除,当删除父表的行时,它将删除关联的子表的行。

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

相关问题 SqlException: DELETE 语句与 REFERENCE 约束“FK_Payment_Student”冲突 - SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_Payment_Student" DELETE语句与REFERENCE约束“FK_Claims_Patients”冲突 - The DELETE statement conflicted with the REFERENCE constraint “FK_Claims_Patients” DELETE语句与REFERENCE约束冲突 - The DELETE statement conflicted with the REFERENCE constraint DELETE语句与REFERENCE约束冲突 - DELETE statement conflicted with REFERENCE constraint 如何捕获与REFERENCE约束FK冲突的语句 - How do I catch statement conflicted with the REFERENCE constraint FK DELETE语句与sql中的REFERENCE约束冲突? - The DELETE statement conflicted with the REFERENCE constraint in sql? 错误:DELETE语句与REFERENCE约束冲突 - Error: The DELETE statement conflicted with the REFERENCE constraint 如果“ DELETE语句与REFERENCE约束冲突”,则取消“删除”事件 - Canceling a “delete” event if “The DELETE statement conflicted with the REFERENCE constraint” C# 实体框架,删除子项然后父项,DELETE 语句与 REFERENCE 约束冲突 - C# Entity Framework, deleting child items then parent, The DELETE statement conflicted with the REFERENCE constraint ASP.NET-DbUpdateException:DELETE语句与REFERENCE约束冲突 - ASP.NET - DbUpdateException : DELETE statement conflicted with the REFERENCE constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM