Skip to content

How to update foreign key back to NULL?

nave

Asked Oct 21, 2013, 1:54 AM EDT2 answers
Share
LinkedInX
How do i update a foreign key column back to null?

On insert or update i use this:

getCategory = _db.Categories.Find(id);
Events.Categories = getCategory; //add foreign key

But if the category is 0 (not selected) i want to update the relationship Category_ID back to null.

Try to do it like that:

Events.Categories = null; //add foreign key

But it doesn't seems to work

Please advise
Thanks

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Sandeep Singh Shekhawat
    0
    A table foreign key same as primary key for another table so in sql server the primary key field can not take a null value. So we can't insert null value in foreign key field as well.

    Thanks,
    Sandeep
    0
  • 0
    Thanks for the replay but when i insert the item with a 0 in the categoryId i get a NULL on the Category_ID DB column which is the foreign key.

    So is there a way to delete the relationship?

    Please advise
    Thanks
    0