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

Delete Orphan - 1:1 relationship

3 Answers 61 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.
David Hollins
Top achievements
Rank 1
David Hollins asked on 27 Apr 2011, 04:25 PM
In a simple class which has a reference to a single child, if that child is replaced, the original reference is orphaned.

Example:
class Parent
{
   private Child _myChild;
 
   public Child myChild
   {
      get { return _myChild; }
      set { _myChild = value; }   // original is orphaned when a new instance is set
   }
}

Now, if in code I set this object, the original is orphaned because the reference is overwritten with the new object.  Without specific management, how can this be solved similar to the delete-orphans setting if this would have have been a collection?

I would like to specify the delete-orphans on any child object that I want cleaned up when all references to it are removed.

Thanks,
David Hollins

3 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 03 May 2011, 08:20 AM
Hello David Hollins,

 Unfortunately there is no such setting like delete orphan in a 1:1 relation, it is only available with the collections. Here you can write a simple logic in order to achieve that by yourself.

class Parent
{
   private Child _myChild;
  
   public Child myChild
   {
      get { return _myChild; }
      set {
          IObjectContext context = Database.GetContext(this);
          context.Remove(_myChild);
          _myChild = value;
        }
   }
}

Best wishes,
Zoran
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
David Hollins
Top achievements
Rank 1
answered on 03 May 2011, 12:35 PM
My problem comes from the fact that we don't have any "database" logic in our classes outside of the attributes placed by the Forward Mapping Wizard.  All persistence is done via a Repository pattern and not internal to the classes.  So access to the database object and context is not known to the class from which the object will be removed.

Is there a way to plug into the system or register callbacks to allow me to intercept the action and perform the behavior I want to perform?
0
Zoran
Telerik team
answered on 06 May 2011, 09:39 AM
Hello David Hollins,

 You do not have to worry about the fact that your persistent classes do not know about their scope or the database object which you handle on different layer of your application. The code I have sent you uses not your instance Database object which for which the persistent types do not know, but calls the a static method from the Database class and that is something for which your objects must know as you have a reference to the Telerik.OpenAccess.dll assembly.

Best wishes,
Zoran
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
Tags
General Discussions
Asked by
David Hollins
Top achievements
Rank 1
Answers by
Zoran
Telerik team
David Hollins
Top achievements
Rank 1
Share this question
or