Weebs
Weebs

Reputation: 435

Entity Framework: Call a function after an object is removed from the database

I'm currently working on an ASP.NET MVC project that allows the user to upload files to the server, and associate them with another object

public class FileAttachment
{
    [Key]
    public int Id { get; set; }

    public string Title { get; set; }
    public string ContentType { get; set; }
    public string Extension { get; set; }

    [ForeignKey("Donor")]
    public int DonorId { get; set; }
    public virtual Donor Donor { get; set; }
}

public class Donor
{
    [Key]
    public int Id { get; set; }

    // .....

    public virtual List<FileAttachment> Attachments { get; set; }
}

What I'm wondering is if there's a way to specify a function for Entity Framework to execute when a Donor or FileAttachment object is removed from the DbSet, because the attachments are stored in the file system so I need to make sure the file gets deleted.

Upvotes: 0

Views: 169

Answers (1)

Jacob
Jacob

Reputation: 78840

Databases don't typically call applications to notify when their data changes. It would be much easier if you design your database so it is managing the attachment files, not your application.

I'm not sure what database you're using, but if you're using Microsoft SQL Server, you should look into using the FileStream data type.

Upvotes: 1

Related Questions