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

Problem Deleting Association In Many-to-Many Relationship

2 Answers 214 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.
Solomon
Top achievements
Rank 1
Solomon asked on 04 Aug 2010, 11:30 AM
I 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 to I check the Many To Many checkbox). I can walk the relationships fine in my code (example 1), and add associations in the join table (by using the Add method and committing), but I am having big problems 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 object from the event table also - not just the association in the speaker_event table). In the case, I decorated the private IList<Event> member in the persistent object.

Alternatively, 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. Anyone have 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


2 Answers, 1 is accepted

Sort by
0
Serge
Telerik team
answered on 13 Aug 2010, 04:23 PM
Hi Solomon,

 First of all I would like to apologize for the late reply, this indeed is not our practice.

In order to achieve your goal you need to specify that the many to many collection is managed, this can be done through the forward mapping wizard or via the app.config. You can read more on this issue in this help article. Also please make sure that there are no depend attributes left out because they would bring havoc into your data, what they do is specify that when you delete a speaker the corresponding event will also be deleted.

I do hope this helps and again I am really sorry for the late reply.

Sincerely yours,
Serge
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
0
Solomon
Top achievements
Rank 1
answered on 21 Jan 2011, 08:32 PM
Tags
General Discussions
Asked by
Solomon
Top achievements
Rank 1
Answers by
Serge
Telerik team
Solomon
Top achievements
Rank 1
Share this question
or