none
Deleting child object RRS feed

  • Question

  • Hi there

    I'm having a nightmare with LINQ at the moment. My database has Document and DocumentPage tables that are related.
    The Document table has a Document_ID as its primary key and the DocumentPage has a composite primary key of Page_ID and Document_ID. Document_ID in the DocumentPage table is the foriegn key of the relationship that cannot be set to null.

    The application that I am building creates a Document that has a collection DocumentPage. The user can add, delete and replace pages in the document. I am having an issue with LINQ when I try and delete or replace (delete then add) a page in the document which throws the exception:

    An attempt was made to remove a relationship between a Document and a DocumentPage. However, one of the relationship's foreign keys (DocumentPage.Document_ID) cannot be set to null.   
    at System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.SynchDependentData()    at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)    at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)    at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)    at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)    at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects()    at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)    at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)    at System.Data.Linq.DataContext.SubmitChanges()    at Scanner.UploadSystem.UploadDocument(String file) in C:\Users\Ian\Documents\Visual Studio 2008\Projects\Project7\Scanner\Upload\UploadSystem.vb:line 57

    I found Beth Massi's Blog - Sharing the goodness that is VB and in addition to setting DeleteOnNull=”‘true” for the child table in the .dbml file, I also used the option to enable cascading deletes on the parent. But I'm still having the same issue.

    Please help.

    Regards
    Monday, July 7, 2008 4:04 PM

All replies

  • Can you show us the code you are using to delete and add the new objects and at which point you are calling submit changes?

     

    Thanks,

     

    [)amien

     

    Monday, July 7, 2008 11:06 PM
    Moderator
  • Enforcing a Cascade Delete constraint in DB between the concerned tables and recreating your schema might help.

    Tuesday, July 8, 2008 12:32 AM
  • Hi there

    At the startup of the program I instantiate a new Document object:

    Code Snippet

    mCurrentDocument = New Document
    mCurrentDocument.DocumentPages = New System.Data.Linq.EntitySet(Of DocumentPage)


    To add a newly scanned page to the document I use:

    Code Snippet

    CurrentDocument.DocumentPages.Insert _
            (aFinishScanParts.IntPageIndex, New DocumentPage With {.DocumentStatus = _
                      (From s In mScannerSystem.DBLayer.DocumentStatus _
                       Where s.DocStatus_Name.ToLower = "final" _
                       Select s).Single, .DocPage_ID = aFinishScanParts.IntPageIndex + 1})



    where aFinishScanParts is a structure containing the information for thenewly scanned page.

    To delete a page I use:

    Code Snippet

    CurrentDocument.DocumentPages.RemoveAt(index)


    Once the user is entirely satisfied with the document, then only do I commit the Document with all its DocumentPages to the database using:

    Code Snippet

    mScannerSystem.DBLayer.Documents.InsertOnSubmit(mScannerSystem.CurrentDocument)
    mScannerSystem.DBLayer.SubmitChanges()


    To replace a specific page in the document merely involves calling the delete and then the add. So I figure it's the delete that's giving me the errrors.

    I have cascaded the delete and rebuilt the schema to no avail.
    Tuesday, July 8, 2008 8:17 AM
  • What am I doing wrong here?
    Friday, July 11, 2008 10:02 AM