【问题标题】:Entity Framework: Call a function after an object is removed from the database实体框架:从数据库中删除对象后调用函数
【发布时间】:2013-11-05 22:02:48
【问题描述】:

我目前正在开发一个 ASP.NET MVC 项目,该项目允许用户将文件上传到服务器,并将它们与另一个对象相关联

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; }
}

我想知道的是,是否有办法指定实体框架在从 DbSet 中删除 Donor 或 FileAttachment 对象时执行的函数,因为附件存储在文件系统中,所以我需要确保文件被删除。

【问题讨论】:

标签: c# asp.net-mvc entity-framework


【解决方案1】:

数据库通常不会调用应用程序来通知它们的数据何时发生变化。如果您将数据库设计为 管理附件文件,而不是您的应用程序,这会容易得多。

我不确定您使用的是什么数据库,但如果您使用的是 Microsoft SQL Server,您应该考虑使用 FileStream data type

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    相关资源
    最近更新 更多