Ticket Q456052
Visible to All Users

Deferred deletion removes references

created 12 years ago

Hello,
I have some problems with XPO object library.

I have a class Order and a class OrderItem. OrderItem has a reference to Order.
Simplified versions of these objects look like this:

public class Order : XPCustomObject
{
[Key(AutoGenerate = true)]
public Guid Oid
}

[DeferredDeletion(true)]
public class OrderItem : XPCustomObject
{
[Key(AutoGenerate = true)]
public Guid Oid

Order order;
[Association("Order_OrderItem", typeof(Order))]
public Order Order
{
get { return order; }
set { SetPropertyValue<Order>("Order", ref order, value); }
}
}

Once the objects are created and saved, the corresponding records in database tables occur as well.

If I set Order to the OrderItem, it's Oid shows in the table as a reference. That is still alright.

Then if I delete OrderItem with the reference to Order, the record stays in the database (as expected and with GCRecord set to not null), but the reference to Order disappears (column Order is null suddenly).

Later if i want to restore all the deleted OrderItems of an Order, I cannot do it, because there are none with the Order.Oid set in the Order column.

Can you please help me with deferred deletion not losing the references between objects?

Thank you in advance, Jan

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hi Jan,
This behavior is by design. If we do not clear reference properties within a deleted object the foreign key constraint will prevent deleting the referenced object (if it does not use deferred deletion too).
Although the Deferred Deletion feature was not designed to recover deleted objects, you can override the OnDeleting method to backup referenced object keys to additional persistent properties. For example:

C#
[Browsable(false)] public Guid? OrderKey { get; set; } protected override void OnDeleting() { OrderKey = (Order == null) ? (Guid?)null : Order.Oid; base.OnDeleting(); } public void Restore() { if (OrderKey.HasValue) { Order = Session.GetObjectByKey<Order>(OrderKey.Value); } SetMemberValue("GCRecord", null); }

    Other Answers

    created 12 years ago (modified 12 years ago)

    This does the trick:

    C#
    Order _order; [Association("Order_OrderItem), Aggregated] public Order Order { get { return _order; } set { if(value == null && IsDeleted) return; SetPropertyValue("Order", ref _order, value); } }

    HTH, Robert

      Comments (3)

        This works, thank you very much

          What are the implications of using Robert Fuch's solution over the custom IObjectLayer solution Anatol posted in t269498? Anatol's solution relies on an extra Boolean IsSoftDeleted, while RTobert's solution seems to not require anything extra. Are there any downsides to Robert's solution?

          DevExpress Support Team 4 years ago

            Hello Adrian,

            I created a separate ticket on your behalf: T1011122: Why a child object clears a reference to a parent object when being deleted?. We placed it in our processing queue and will process it shortly.

            Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

            Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.