none
An attempt was made to remove a relationship between a ??? and a ??? However, one of the relationship's foreign keys (???) cannot be set to null. RRS feed

  • General discussion

  • Hi,
    In my latest code, I had used the following methods to insert and delete a child entity:

    AddPhone(Phone phone)
    {
     Contact.Phones.Add(phone);
    }
    DeletePhone(Phone phone) { Contact.Phones.Remove(phone); }

    But above codes causes the following exception when SubmitChanges method is called:
    "An attempt was made to remove a relationship between a Contact and a Phone However, one of the relationship's foreign keys (Phone.ContactID) cannot be set to null."

    After too much coding, finally I found a way to resolve the exception.
    I added an additional line to both add and delete methods:

    AddPhone(Phone phone)
    {
     Contact.Phones.Add(phone);
     dataContext.GetTable<Phone>().InsertOnSubmit(phone); 
    }
    
    DeletePhone(Phone phone);
    {
     dataContext.GetTable<Phone>().DeleteOnSubmit(phone); 
     Contact.Phones.Remove(phone);
    }
    WOOW, IT WORKED :)
    Monday, October 18, 2010 5:33 PM

All replies