I have a SQL association table that has just two columns which are both foreign keys (ID's of twos from two other tables) that represents a relationship between two data objects. It seems natural that those columns have a NOT NULL constraint, because it would seem meaningless to have a relationship between an object and null, or between null and null.
What surprises me, is that when my (WPF, LINQ, C#) program tries to clear an object's associations, the following submit raises an exception saying one of the columns can't be null. I don't want it to be null, I want the whole row to be cleared.
Now, I could drop the null constraint to bypass the exception, but that seems like it shouldn't be necessary.
Edit: actually, SQL Server won't let me do only that, because the two values are the primary key, and a null isn't allowed in a primary key, which makes sense ... but again, I'm trying to remove rows, not set their ID's to null.
So I ask:
Why is removing rows (via clear()
) resulting in an exception about a null column value?
and
Is there some other way for me to clear such rows that I should be doing, or do I need to remove the not null constraint?
Or is there something else I am not understanding?
Details:
My table has two columns:
RecipeID (PK, FK, int, not null)
AltRecipeID (PK, FK, int, not null)
C# LINQ-to-SQL code:
RecipeToUpdate.PFW_AlternateRecipes.Clear();
App.db.SubmitChanges();
Exception on submit:
An attempt was made to remove a relationship between a PFW_Recipe and a PFW_AlternateRecipe. However, one of the relationship's foreign keys (PFW_AlternateRecipe.RecipeID) cannot be set to null.