Ticket T1279931
Visible to All Users

How to customizely handle object deletion in XAF Web Blazor application using Entity Framework?

created 3 days ago (modified 3 days ago)

Hello there,

I have a class responsible to upload, download and delete files to a cloud storage (Azure). To upload the files I can override the OnSaving() method in which I call the storageHelper Upload method to store the file in the cloud (see code below). However I can't find any method to override when the object is deleted, as I want to delete it as well from the cloud storage.

In XPO objects you have got the OnDeleting() method. Is there any similar method for EF objects? If not, do you have any suggestions for a solution?

This is the Class I'm talking about:

Code
public class FileDataReference : BaseObject, IFileData, IEmptyCheckable { private readonly AzureStorageHelper storageHelper; public virtual Guid FileId { get; set; } public virtual string? FileName { get; set; } public virtual int Size { get; set; } public virtual long SizeLong { get; set; } public virtual required string Container { get; set; } public virtual bool IsEmpty => Size == 0; string? fileToClear; byte[]? bytesToSave; public FileDataReference() { storageHelper = new AzureStorageHelper(); } public override void OnCreated() { base.OnCreated(); FileId = Guid.NewGuid(); Container = "images"; } public void Clear() { bytesToSave = null; fileToClear = FileName; FileName = null; Size = 0; SizeLong = 0; } public void LoadFromStream(string fileName, Stream stream) { using var ms = new MemoryStream(); stream.CopyTo(ms); FileName = fileName; Size = stream.Length <= long.MaxValue ? ((int)stream.Length) : int.MaxValue; SizeLong = stream.Length; bytesToSave = ms.ToArray(); fileToClear = null; } public void SaveToStream(Stream stream) { storageHelper.Download(Container, FileId, ref stream); } public override void OnSaving() { base.OnSaving(); storageHelper.Upload(Container, FileId, FileName, bytesToSave); bytesToSave = null; } public override void Deleting() ===> **This method does not exist!** { Clear(); storageHelper.Delete(Container, FileId); } }

Thank you in advance,
Kind regards,
Herman.

Answers approved by DevExpress Support

created 2 days ago

Hello Herman,

Thank you for the code snippet.

While our base classes and interfaces for EF Core business classes do not have an alternative to XPO's OnDeleting method, there are other ways to handle this usage scenario in XAF. Please refer to the following help topic for more information: Delete Objects from the Database.

If this information doesn't help or I misunderstood your usage scenario, please describe it in greater detail or create a small sample project that illustrates what you're trying to achieve.

Best regards,
Herman

    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.