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

Remove method does not seem to work

10 Answers 221 Views
OQL (OQL specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
hkdave95
Top achievements
Rank 2
hkdave95 asked on 09 Sep 2009, 07:46 AM
Hi

I am using the latest version of ORM.

I have a remove method to delete two rows from a table object.

i.e. Table1[0].Teble2.Remove(Object)

During debugging I can see the row being removed and the Table2 count being reduced.

The Transaction.Commit is occurring using your Scope "persistence" Module handler example.

BUT the rows are not being removed.

From the above can you see if I am doing anything wrong.

Kind Regards

David

10 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 09 Sep 2009, 09:22 AM
Hi David,

I have to recap on your classes..

What is Table1 and what is Teble2... is it a list or?

I guessi it is a list. Remove actually removes it from the collection it does not remove the instance of the class. This is configuration specific, thus you can modify this behavior... See the manage collection settings and Depend() attribute in the docs.

/Henrik
0
hkdave95
Top achievements
Rank 2
answered on 09 Sep 2009, 10:27 AM
Hi Henrik

Table1 is an IQuery, the result of a OSQL query.

Table2 is a "linked" table within that.

I am not near the code at the moment so I cannot be more explicit I am afraid.

The Depend() attribute seems to be to do with cascaded deletes which is not relevant in this example.

Kind Regards

David

0
PetarP
Telerik team
answered on 11 Sep 2009, 08:16 AM
Hi hkdave95,

Henrik is right. Your problem is indeed caused by the remove method. This method would remove an item from the collection but it won’t delete it from the database. To create a delete mechanism for the items in the collection when you call the Remove method you will need to implement few lines of code. I believe that you will find the following blog post useful. Note that there is also a code library demonstrating the approach described in the post. Please feel free to contact us if any further difficulties arise.


Greetings,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
IT-Als
Top achievements
Rank 1
answered on 11 Sep 2009, 08:43 AM
Hi David and Petar,

Another way could be using the <delete-orphans> of the many side class if (and only if) it can only be referenced by one type of parent class (like in order-orderline class relationship, where <delete-orphans> will be applied to the orderline class). This means, that if an orderline is removed from the order.lines collection with Remove method it also removes the instance of the orderline class as opposed to only removing the reference to it when not using <delete-orphans>.

To generalize:
In 1:m relationships where the 1-side class has an owning relationship to the many-side one can benifit from using the <delete-orphans> on the many side class.

Regards

Henrik

0
PetarP
Telerik team
answered on 16 Sep 2009, 04:22 PM
Hello Henrik,

The delete-orphan option will only work if you have managed collection switched on as well. In such cases every object that is left without a parent (e.g. removed from the parents collection) will be deleted from the database. Using the Parent.Collection.Clear() method will remove all its children.

Regards,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Solomon
Top achievements
Rank 1
answered on 04 Aug 2010, 11:25 AM
I am trying to do something similar, but am having ALOT of difficult with many-to-many relationships. I reverse mapped my database which has many join tables. I denote these as many-to-many relationships in my reverse mapping wizard (the wizard rightfully does not pick these up automatically). I can walk the relationships fine in my code (example 1), but I am having difficulty deleting the association. Everything I try deletes the other object that the join table is joining to.

For example, I have an Event object and a Speaker object with a join table (an Event can have multiple speakers and a speaker can speak at multiple events). Obviously I need to delete the association when I delete an object or I get a database constraint error. I tried a Depends attribute to cascade the deletes, but it actually deletes the other object (when I delete a speaker, it deletes the event also - not the speaker_event association). I decorated the private IList<Event> member in the persistent object. When I iterate through the collection and Remove each object (example 2), it does not persist to the database. I tried the solution Here, but it also deletes the other object - not the association. Because OpenAccess does not generate a SpeakerEvent object (based on the speaker_event join table), there does not seem to be any way to access just that relationship to remove it. It works great with the Add (it is not like it adds a new event, it adds a new speaker_event association). 

This seems like basic functionality that is included in Entity Framework and LINQ to SQL. Any thoughts on this? Very frustrating. Any ideas?

example 1
foreach (Speaker speaker in event.Speakers)
{
    var item = new RadComboBoxItem {Text = speaker.Name, Value = speaker.speakerId.ToString()};
    lstSpeakers.Items.Add(item);
}

example 2
_speakerHelper.StartTransaction(); //starts a transaction using httpcontext
 
var speaker = _speakerHelper.GetSpeakerById(1);
 
for (int i = 0; i < speaker.events.Count; i++)
{
        speaker.events.Remove(speaker.events[i]); //I would like to do something like this and remove the ASSOCIATION, not the actual event object
}
 
_speakerHelper.CommitTransaction(); //commits the transaction using httpcontext
0
IT-Als
Top achievements
Rank 1
answered on 09 Aug 2010, 12:17 PM
Hi Solomon,

I would suspect it has something to do with the relationship not been "managed". That is, you have to manage it yourself - on both ends.

However, did you try to use Clear() on the collection instead of removing them one by one?

Regards

Henrik
0
Solomon
Top achievements
Rank 1
answered on 10 Aug 2010, 06:29 AM
Sorry Henrik, can you expand on that? I do not see any option for "managed" relationships in the reverse engineering wizard. I have tried the Clear() method without any success. 

Let me make sure I have this correct... just to make sure... I have three tables - a session table, speaker table, and a session_speaker table. The session_speaker table has two columns (both PKs) pointing back to their respective owners. I have marked each of these relationships as many-to-many in the reverse engineering wizard (and marked the Inverse Field property as "Session" so I can say Speaker.Sessions or Session.Speakers and not Speaker.SpeakerSessions). Other than that, everything is pretty standard. Please let me know if you would like to see some code.

Thanks!

Sol
0
IT-Als
Top achievements
Rank 1
answered on 10 Aug 2010, 08:01 AM
Hi Solomon,

Code is always good :-)

I don't if you are aware, but doing reverse mapping does not exclude you from using forward mapping after that (kind of round-tripping,  like in the new visual designer)

So, what you can do is:

1) Reverse mapping (guess you have already done that)
2) Open the forward mapping wizard on your reverse mapped model
3) Mark your collections as being managed on your Speaker class and Session class

As I understand it you don't actually want to dispose/delete any of the Speaker and Session instances, right?
What you want to do is remove the association between them

After doing the above steps, it should be enough to remove from one of the collections (Speaker or Session class) and the other will get updated automatically by OA

Regards

Henrik
0
PetarP
Telerik team
answered on 01 Sep 2010, 03:56 PM
Hello Solomon,

 What Henrik suggested is perfectly valid and should work in your case. If it doesn't can you please perhaps provide us with some additional code in order for us to provide you with a better solution.

All the best,
Petar
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
OQL (OQL specific questions)
Asked by
hkdave95
Top achievements
Rank 2
Answers by
IT-Als
Top achievements
Rank 1
hkdave95
Top achievements
Rank 2
PetarP
Telerik team
Solomon
Top achievements
Rank 1
Share this question
or