This is a migrated thread and some comments may be shown as answers.

[Solved] Deleting in a 1:M situation?

4 Answers 137 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
shawn reagan
Top achievements
Rank 1
shawn reagan asked on 05 Nov 2010, 05:28 AM
Okay.
I am using OpenAccess forward mapping and have the following scenario.

ObjectA

ObjectB

There is a one to many relationship between ObjectA and ObjectB.

I can create ObjectA and an instance of ObjectB with no problem. I then add ObjectB to the collection on Object which is is basically the 1:m relationship and this is fine also.  My issue comes up when I  delete ObjectA.  Currently when I delete ObjectA it also deletes ObjectB as well.  What I need is for the mapping to be deleted but to not actually delete ObjectB.

I'm currently doing that this way, but I feel there may be a better implementation.  Any thoughts?

Thank you.

' grab the scope
Dim scope As IObjectScope = ScopeProvider.ObjectScope
scope.Transaction.Begin()
 
' fetch objectA from the scope that is to be deleted
Dim si As ObjectA = ObjectA.GetItem(Of ObjectA)(scope, Id)
 
' we need to break the association of any ObjectB to the ObjectA before we actually delete the ObjectA
Do While si.ObjectBCollection.Count > 0
    si.ObjectBCollection.Remove(si.ObjectBCollection(0))
Loop
 
' physically delete ObjectA
scope.Remove(si)
 
scope.Transaction.Commit()

4 Answers, 1 is accepted

Sort by
0
Accepted
IT-Als
Top achievements
Rank 1
answered on 05 Nov 2010, 10:26 AM
Hi Shawn,

How is the 1:n association defined. Are you using a join table or a foreign key (inverse field) for the mapping?

You should check if you have any [Depend] attributes on the field or other means of cascading deletes. To my understanding these are disabled by default, so by default a child object inside a collection of another object is not deleted when you delete the parent object.

Did you try using the Clear() method of the collection instead of the while loop.

Regards

Henrik
0
shawn reagan
Top achievements
Rank 1
answered on 05 Nov 2010, 02:36 PM
Thank you for the quick reply.  I forgot to check for the "Depend" attribute.  I'll have to check the code later tonight, but that may be it.  I messed around with that along time ago and I'm not sure if I ever got rid of it.

I'll try to post back with details later tonight.

Thank you.
0
shawn reagan
Top achievements
Rank 1
answered on 05 Nov 2010, 08:10 PM
Thank you Henrik!  It does appear there was a "Depend" attribute hiding in there I was unaware of.  I appreciate the help.  It looks like it all works as expected.

Shawn
0
IT-Als
Top achievements
Rank 1
answered on 05 Nov 2010, 08:49 PM
Hi Shawn,

Glad I could help.

Please mark the the question as answered, so that other forum visitors can easily find the answer for a similar question.

Best regards

Henrik
Tags
General Discussions
Asked by
shawn reagan
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
shawn reagan
Top achievements
Rank 1
Share this question
or