The UPDATE statement conflicted with the FOREIGN KEY constraint


When I am trying to save Updated data in table using Entity Framework in ASP.NET MVC, I am getting the below error

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_CustomerProfileV2_IndustryList". 
The conflict occurred in database "CRM", table "dbo.IndustryList", column 'IndustryName'.
The statement has been terminated.

How can i Resolve the above issue?

Here is my Current sample code

 [HttpPost]
        public ActionResult EditCustomer(long customerProfileID, CustomerProfile model)
        {
            try
            {
                //code removed 

                crmdb.Entry(model).State = System.Data.Entity.EntityState.Modified;
                crmdb.SaveChanges(); //error here

               //some code removed
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString().Replace("\n", "<br>"));
                return PartialView();
            }
        }

Any help is appreciated, thanks


Asked by:- jon
0
: 9582 At:- 4/2/2018 9:33:50 AM
MVC asp.net mvc C#







2 Answers
profileImage Answered by:- manish

As per your error details "The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_CustomerProfileV2_IndustryList". The conflict occurred in database "CRM", table "dbo.IndustryList", column 'IndustryName'. The statement has been terminated."

When you are submitting forms or updating the data, You have a field related to Table dbo.IndustryList with Columns name IndustryName, which cannot be empty due to Foreign key constraint.

So to simply resolve this error provide some value for IndustryName in your form

2
At:- 4/2/2018 11:29:46 AM
Thanks, I see I had conflicts, I wasn't passing value for IndustryName using dropdown list which was required and after passing the value properly error was resolved. 0
By : jon - at :- 4/4/2018 7:19:21 AM


profileImage Answered by:- bhanu

Above answer works in some cases but, this error can also occur when you try to update "Primary Key" of a table row but it is referenced by a foreign key from another table and the UPDATE SPECIFIC of table Reference is set to "No action".

So to remove this error, in your SSMS database table, you can simply right-click your foreign key and select Modify.

In the Foreign key relationships dialog under the "INSERT and UPDATE Specific" set the UPDATE rule =  Cascade (Instead of "No Action")

You can also perform above operation using SQL Query

ALTER TABLE YourTable
DROP Constraint Your_FK
GO

ALTER TABLE YourTable
ADD CONSTRAINT [New_FK_Constraint]
FOREIGN KEY (YourColumn) REFERENCES ReferencedTable(YourColumn)
ON DELETE CASCADE ON UPDATE CASCADE
GO 
0
At:- 8/3/2021 3:29:06 PM Updated at:- 8/3/2021 3:30:38 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use