1

I would like to know if is there any posibility to catch something like "OnDelete Event" for cascading deletction of each entity. All classes have implemented cascading and it works flawlessly (removing course removes file RECORDS in DB). My struct is like this:

publi class Course
{
    ...
    public virtual ICollection<Lesson> Lessons {get; set;}
    public Course()
    {
        Lessons = new List<Lesson>();
    }
}

public class Lesson
{
    ...
    public virtual ICollection<Section> Section {get; set;}
    public Lesson()
    {
        Section = new List<Section>();
    }
}

public class Section
{
    ...
    public virtual File File {get; set;}
}

public class File
{
    ...
    public Uri Uri {get; set;}
    // I Would like to catch every delete of file to remove it from server.
}

Now when I remove any lesson from my course i want all files (phisical files on server) related to that lesson (related through Section) to be deleted.

Same problem had @derrick in comment of this solution: Model OnDelete event?

Community
  • 1
  • 1
Artur Szymański
  • 1,639
  • 1
  • 19
  • 22

1 Answers1

1

The answer to this question might shed some light on your issue: Cascading deletes with Entity Framework - Related entities deleted by EF

Basically, EF doesn't have a mechanism to know about what's being deleted by the SQL server if the cascade is being executed by the SQL server (just as it won't know about what is being deleted in another instance of the application connected to the same server), if the entity isn't being deleted by EF directly, it can't know it's being deleted.

The only real workaround I know about is to perform the cascade yourself, in which case you could extend the record delete function to also delete any physical files on the server.

Community
  • 1
  • 1
AllMadHare
  • 370
  • 3
  • 15