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
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
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
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly