Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
delete-orphans
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > Metadata Extension Reference > delete-orphans

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

The ”delete-orphans” option is a boolean class-level option to control the automatic deletion of certain types of unreferenced instances. If the delete-orphans extension is set to true, and the class is involved on the ‘many side’ of a relation, then it is deleted on commit if all of the reference fields used on the opposite side of the relationship(s) are null.
For example let’s take the parent/child relationship between Order and OrderDetails: the order should have a collection of order details.

C# Copy Code
[Telerik.OpenAccess.Persistent()]
class Order
{
private IList<OrderDetail> orderDetails = new List<OrderDetail>();
public IList<OrderDetail> OrderDetails
{
 get
 {
  
return orderDetails;
 }
 set
 {
  orderDetails = value;
 }
}
}
[Telerik.OpenAccess.Persistent()]
class OrderDetail
{
public string name;
}
VB.NET Copy Code
<Telerik.OpenAccess.Persistent()> _
Friend Class Order
 Private orderLines_Renamed As IList(Of OrderDetail) = New List(Of OrderDetail)()
 Public Property OrderLines() As IList(Of OrderDetail)
  Get
   Return orderLines_Renamed
  End Get
  Set(ByVal value As IList(Of OrderDetail))
   orderLines_Renamed = value
  End Set
 End Property
End Class
<Telerik.OpenAccess.Persistent()> _
Friend Class OrderDetail
 Public name As String
End Class

 When you remove an item from the collection of the order object (without removing it from the scope) and commit, then the order detail instance will be left without a parent (aka orphan). In this case we removed the item from the collection but it still exists in the database. If the delete orphan is active then this order detail will be removed from the database as well (else it will still reside in the database even though it is not linked to anything).

 

C# Copy Code
IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
scope.Transaction.Begin();
Order o = scope.Extent<Order>().First();
//In case of Clear() all the data from OrderDetails table will
//be deleted from the database.
//if the delete-orphans is set to true.
o.OrderDetails.Clear();
scope.Transaction.Commit();
VB.NET Copy Code
Dim scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope()
scope.Transaction.Begin()
Dim o As Order = scope.Extent(Of Order)().First()
'In case of Clear() all the data from OrderDetails table will
'be deleted from the database.
'if the delete-orphans is set to true.
o.OrderDetails.Clear()
scope.Transaction.Commit()
The both examples will remove only instance if you use Remove method instead of Clear.

If you use this option you should not set the references to the persistent values in a collection as dependent; as instances used by one collection might be deleted when the other collection is deleted, which will cause an ObjectNotFoundException when the collection is retrieved.

The XML metadata for the same is given below:

Copy Code
<class name="OrderDetail">
    <extension key="delete-orphans" value="true" />
</class>