To get started, let's just start with my mistake:
"An attempt was made to remove the relationship between X and Y. However, one of the external external links cannot be set to null."
Now to explain what I'm trying to do ...
I have the following database tables: Sample, Male, Female, Sample. A man is always a model, and a female is always a model. Thus, a pattern has two one-to-many relationships (but logic prevents selection of a pattern). In addition, a man can have one-to-many girlfriends, and a woman can have one-to-many guys (hey, this is the 21st century after all). This was solved by creating a many-to-many table (SpecimenRelationship).
Relations in SQL are set up so that the pattern removes the cascades for both men and women. After that, the desired function is designed to remove the Male / Female for the cascade in the SpecimenRelationship, but due to SQL restrictions (multiple garbage cycle!) This is not done. So, one cascade and the other is SetNull (let's say Male is SetNull).
Now for a bit where everything goes wrong. When I remove the SpecimenRelationship from the Male object, I get the error above. But why is this happening? I don’t see where I even delete the male entity, don’t I understand how Linq-to-Sql works, why is it not just a direct deletion of the SpecimenRelationship record?
Here is a sample code:
Male male = GetMaleFromDataContext();
SpecimenRelationship relationshipToRemove = male.SpecimenRelationships.Single(x => x.FemaleID == someFemaleID);
male.SpecimenRelationships.Remove(relationshipToRemove);
DB.SubmitChanges();
Why does a cascade of relationships even come into play here?
source
share