unexpected behavior on deletemulti()

Posts   
 
    
nilsey
User
Posts: 54
Joined: 11-Jan-2008
# Posted on: 23-Dec-2008 01:32:11   

I was trying to operate on a collection of entities using DeleteMulti()

        Dim user As New UserEntity(Me.UserId)

        ' clear the current assignments from the database for teh selected st body
        Dim filter As New PredicateExpression(ChapterFields.StandardsBodyId = CInt(Me.c_stbody.SelectedValue))

        Dim relations As New RelationCollection()
        relations.Add(UserDisallowedChapterEntity.Relations.ChapterEntityUsingDisallowedChapterId)

        user.UserDisallowedChapter.DeleteMulti(filter, relations)

this is the query which is executed:

DELETE FROM [VSurveyDev].[dbo].[tblUserDisallowedChapter] FROM ( [VSurveyDev].[dbo].[stjcahochapter]  INNER JOIN [VSurveyDev].[dbo].[tblUserDisallowedChapter]  ON  [VSurveyDev].[dbo].[stjcahochapter].[id]=[VSurveyDev].[dbo].[tblUserDisallowedChapter].[DisallowedChapterId]) WHERE ( ( [VSurveyDev].[dbo].[stjcahochapter].[stbody_id] = @StandardsBodyId1))
   Parameter: @StandardsBodyId1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1

Unfortunately, this query deletes all the UserDsiallowedChapters for all users based on the stdbody_id! I would have expected it to **delete only the UserDisallowedChapters in my collection **where the Chapter.StandardBodyId matched my filter, NOT all the UserDisallowedChapters in the database where the filter got a match!!

We found that we had to add the UserId back into the filter in order to get the right result:

        ' clear the current assignments from the database for teh selected st body
        Dim filter As New PredicateExpression(ChapterFields.StandardsBodyId = CInt(Me.c_stbody.SelectedValue))
        filter.Add(UserDisallowedChapterFields.UserId = Me.UserId)

        Dim relations As New RelationCollection()
        relations.Add(UserDisallowedChapterEntity.Relations.ChapterEntityUsingDisallowedChapterId)

        user.UserDisallowedChapter.DeleteMulti(filter, relations)

which gives us this query:

DELETE FROM [VSurveyDev].[dbo].[tblUserDisallowedChapter] FROM ( [VSurveyDev].[dbo].[stjcahochapter]  INNER JOIN [VSurveyDev].[dbo].[tblUserDisallowedChapter]  ON  [VSurveyDev].[dbo].[stjcahochapter].[id]=[VSurveyDev].[dbo].[tblUserDisallowedChapter].[DisallowedChapterId]) WHERE ( ( [VSurveyDev].[dbo].[stjcahochapter].[stbody_id] = @StandardsBodyId1 AND [VSurveyDev].[dbo].[tblUserDisallowedChapter].[UserId] = @UserId2))
   Parameter: @StandardsBodyId1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1.
   Parameter: @UserId2 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 36287.

Is this the way the LLBL generated code is supposed to work? Shouldn't myEntityCollection.DeleteMulti(filter, relations) opreate only on the items in myEntityCollection, not others in the database??

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Dec-2008 05:43:51   

Hi nilsey,

I paste the relevant information about this from the manual:

If you want to delete one or more entities from the persistent storage the same problem as with updating a set of entities appears: you first have to load them into memory, call** Delete()** and they'll be deleted. To delete a set of entities from the persistent storage directly, you can use DeleteMulti() overloads or the *DeleteMultiManyToOne method to achieve your goal. All DeleteMulti() methods work directly on the persistent storage except one, the DeleteMulti() method which does not take any parameters. That one works with the objects inside the collection and deletes them one by one from the persistent storage using an own transaction if the current collection isn't part of an existing transaction. (See for more information about transactions the section Transactions). The DeleteMulti() methods which do accept parameters and thus work on the persistent storage work the same as the UpdateMulti() methods, except of course the DeleteMulti() methods do not accept an entity with changed fields. See for an example how to filter rows for DeleteMulti() the UpdateMulti() example given earlier on this page.

As you can see, this is the expected behavior for DeleteMulti(params) as the delete action is performed directly on the persistence storage.

David Elizondo | LLBLGen Support Team