3 Replies - 1359 Views - Last Post: 12 November 2016 - 09:27 AM

#1 Coding in the name of  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • View blog
  • Posts: 291
  • Joined: 07-July 10

[Solved] Entity Framework Foreign Key Constraint

Posted 11 November 2016 - 09:07 PM

I have an int? type property in my ApplicationUser entity that maps to a Company entity. I'm having a problem where attempting to delete a company gives me this error. Since the CompanyId property in the ApplicationUser is nullable, shouldn't deleting a company just nullify that value? There should be no cascading delete, ApplicationUser records should be able to exist without an accompanying Company record. Can someone shed some light on this for me? Thanks.

"Cannot delete or update a parent row: a foreign key constraint fails (`epc_manager`.`aspnetusers`, CONSTRAINT `FK_AspNetUsers_Companies_CompanyId` FOREIGN KEY (`CompanyId`) REFERENCES `companies` (`Id`))"

This post has been edited by Coding in the name of: 12 November 2016 - 09:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [Solved] Entity Framework Foreign Key Constraint

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,769
  • Joined: 12-December 12

Re: [Solved] Entity Framework Foreign Key Constraint

Posted 11 November 2016 - 10:51 PM

"shouldn't deleting a company just nullify that value?" No. Deleting deletes the row.

Given that it deletes the row, the error message should make sense. You cannot delete a company if that company is referenced elsewhere. To achieve this you would have to reassign the other records to different companies, or nullify the company value (if allowed).

Most often though, you wouldn't do this; you would be removing the history. To signify that a company is no longer active you would have an additional column or two in the company table, 'active' and/or 'date_inactive' (or something similar). Records could be deleted or archived at a later point, but you'd have to move or remove the related records first (without a cascade).
Was This Post Helpful? 2
  • +
  • -

#3 Coding in the name of  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • View blog
  • Posts: 291
  • Joined: 07-July 10

Re: [Solved] Entity Framework Foreign Key Constraint

Posted 12 November 2016 - 09:23 AM

Ok that's very helpful. I will rethink having a delete function, I might add a "deactivate" type function like you said. But if I want to delete a Company I should first null the CompanyId value of its related ApplicationUser entities and the error will be avoided?

This post has been edited by Coding in the name of: 12 November 2016 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,769
  • Joined: 12-December 12

Re: [Solved] Entity Framework Foreign Key Constraint

Posted 12 November 2016 - 09:27 AM

Set it to null or reassign it to another company (or delete the related rows) whatever the reality dictates.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1