Remove method does not seem to work

11 posts, 0 answers
  1. hkdave95
    hkdave95 avatar
    245 posts
    Member since:
    Nov 2006

    Posted 09 Sep 2009 Link to this post

    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
  2. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 09 Sep 2009 Link to this post

    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
  3. DevCraft banner
  4. hkdave95
    hkdave95 avatar
    245 posts
    Member since:
    Nov 2006

    Posted 09 Sep 2009 Link to this post

    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

  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 11 Sep 2009 Link to this post

    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.
  6. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 11 Sep 2009 Link to this post

    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

  7. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 16 Sep 2009 Link to this post

    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.
  8. Solomon
    Solomon avatar
    17 posts
    Member since:
    Feb 2008

    Posted 04 Aug 2010 Link to this post

    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
  9. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 09 Aug 2010 Link to this post

    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
  10. Solomon
    Solomon avatar
    17 posts
    Member since:
    Feb 2008

    Posted 10 Aug 2010 Link to this post

    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
  11. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 10 Aug 2010 Link to this post

    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
  12. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 01 Sep 2010 Link to this post

    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
Back to Top
DevCraft banner