I have two tables:
1. Form with PK = FormId, and
2. CustomerRequestForm that has a nullable reference to Form (FK constraint between CustomerRequestForm.FormId and Form.FormId)
I need to be able to Delete a Form instance and remove the reference from CustomerForm.
I can't seem to find how to do this without getting a FK Constraint Violation.
Within FormEntity I have a collection FormEntity.CustomerRequestForms and in CustomerForm I have the field CustomerForm.Form.
I have tried performing this delete using :
CustomerRequestFormCollection customerForms =
new CustomerRequestFormCollection();
IPredicate filter = CustomerRequestFormFields.FormId == formId;
customerForms.GetMulti(filter);
foreach (CustomerRequestFormEntity custForm in customerForms) {
custForm.Form = null;
// FormId appears as NULL at this point in the debugger
custForm.Save();
}
// Delete the form itself
FormEntity form = new FormEntity(formId);
form.Delete();
I get the following error:
The DELETE statement conflicted with the REFERENCE constraint "FK_CustomerRequestForm_Form". The conflict occurred in database "DBName", table "dbo.CustomerRequestForm", column 'FormId'.
I have also tried removing the CustomerRequestFormEntity objects from form.CustomerRequestForms and saving it, then deleting the form itself.
The FormId column in CustomerRequestForm is nullable and I have also set GenerateNullableFieldsAsNullableTypes to True in my project properties.
What am I missing here?
Thanks.