Delete Object and remove FK reference

Posts   
 
    
colincsb
User
Posts: 18
Joined: 01-Nov-2006
# Posted on: 29-Mar-2007 07:17:19   

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.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Mar-2007 07:43:58   

Instead of setting custForm.Form = null; Set custForm.FormId = null;

colincsb
User
Posts: 18
Joined: 01-Nov-2006
# Posted on: 30-Mar-2007 01:19:35   

Okay ... that worked. I don't understand the difference here.

When I step through the code in the debugger, I thought I saw that doing custForm.Form = null actually sets the FormId property to null as well.

So, why is different if I set it explicitly?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 30-Mar-2007 09:35:29   

It should be working with both... strange. When you do use custForm.Form=null; , is custForm.Fields["FormId"].IsChanged true or false? (as it's set to null, so it's changed).

Frans Bouma | Lead developer LLBLGen Pro
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Mar-2007 09:39:11   

Which LLBLGen Pro runtimeLibrary version are you using?