如何从数据库中永久删除值

How to permanently delete values from the database

我有一个从 javascript 调用的方法,该方法假设永久删除记录,但如果该方法有 DataContext db = new DataContext();,它不会进入该方法,它给出了错误 Internal Server Error

public void PermanantlyDeleteComment(GetCommentInput input)
{
    DataContext db = new DataContext();
    //Follow by the code to delete the comment
}

如果我注释掉 DataContext db = new DataContext(); 断点会进入。

我认为问题出在数据上下文上,但我知道在哪里

这是数据上下文

public DataContext() : base("name=Default")
{
    this.Configuration.AutoDetectChangesEnabled = true;
    this.Configuration.LazyLoadingEnabled = true;
}

我正在使用 DataContext,因为 abp 样板文件不想永久删除,只能软删除,如果您有办法用样板文件硬删除,请告诉我。

本主题的回答:https://forum.aspnetboilerplate.com/viewtopic.php?p=6180#p6193

You can override CancelDeletionForSoftDelete method in your DbContext and prevent cancellation conditionally.

所以,像这样:

protected override void CancelDeletionForSoftDelete(EntityEntry entry)
{
    if (IsSoftDeleteFilterEnabled)
    {
        base.CancelDeletionForSoftDelete(entry);
    }
}

用法:

public void PermanantlyDeleteComment(GetCommentInput input)    
{
    using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.SoftDelete))
    {
        // The code to delete the comment
    }
}

我发现 DataContext 是正确的,只是我的数据库库(有 DataContext.cs)和网络库

有不同的 EntityFramework 版本]