如何使用Entity Framework(EF6)删除Asp.Net MVC 5中的ApplicationUser?

时间:2022-09-18 08:33:10

I'm struggling with deleting an ApplicationUser in my application. I don't mind going with a hard delete, but that has given me a lot of challenges. The user can create posts in which other users can comment. If other users have commented on a user's post, the user can't be hard deleted since the delete is trying to cascade onto the post (which is blocked by the existing comments).

我正在努力在我的应用程序中删除ApplicationUser。我不介意用硬删除,但这给了我很多挑战。用户可以创建其他用户可以评论的帖子。如果其他用户对用户的帖子进行了评论,则无法将用户硬删除,因为删除尝试级联到帖子(由现有评论阻止)。

I'm also ok with a soft-delete approach, but this seems like a nightmare to deal with in a large application. I have found that I have to add if (!user.IsDeleted){...} around anything that mentions the user (comment and post author reference, login, register and almost everything in the Account/Manage controllers) and have to consider adding the check in all future additions to the code.

我也可以使用软删除方法,但这似乎是在大型应用程序中处理的噩梦。我发现我必须在提及用户的任何内容周围添加if(!user.IsDeleted){...}(评论和帖子后作者参考,登录,注册以及帐户/管理控制器中的几乎所有内容)并且必须考虑在将来添加到代码中添加检查。

I have tried searching for established ways of handling user-deletes, but I can't seem to find any (other than quick articles like "how to add "IsDelete" property to your user, which does not go into nearly enough detail of the implications). So is there a proper way to handle user-deletes? It seems like the best way is to start getting my hands dirty with cascading but I'm not really sure where to start.

我试图寻找处理用户删除的既定方法,但我似乎找不到任何(除了快速文章,如“如何添加”IsDelete“属性给你的用户,这没有详细介绍那么有没有一种正确的方法来处理用户删除?看起来最好的办法就是开始让我的手弄脏,但是我不确定从哪里开始。

1 个解决方案

#1


0  

I highly recommend to see the Soft-delete approach which it has been implemented on the ASP.NET Boilerplate.It is Free and Opensource.

我强烈建议看看它已经在ASP.NET Boilerplate上实现的软删除方法。它是Free和Opensource。

Soft-delete filter is used to automatically filter (extract from results) deleted entities while querying database. If an entity should be soft-deleted, it must implement ISoftDelete interface which defines only IsDeleted property.

软删除过滤器用于在查询数据库时自动过滤(从结果中提取)已删除的实体。如果实体应该被软删除,则必须实现ISoftDelete接口,该接口仅定义IsDeleted属性。

Ex :

public class Person : Entity, ISoftDelete
{
    public virtual string Name { get; set; }

    public virtual bool IsDeleted { get; set; }
}

Here is the Link : ISoftDelete

这是Link:ISoftDelete

Source Code on Github

Github上的源代码

ASP.NET Boilerplate Documentation

ASP.NET Boilerplate文档

#1


0  

I highly recommend to see the Soft-delete approach which it has been implemented on the ASP.NET Boilerplate.It is Free and Opensource.

我强烈建议看看它已经在ASP.NET Boilerplate上实现的软删除方法。它是Free和Opensource。

Soft-delete filter is used to automatically filter (extract from results) deleted entities while querying database. If an entity should be soft-deleted, it must implement ISoftDelete interface which defines only IsDeleted property.

软删除过滤器用于在查询数据库时自动过滤(从结果中提取)已删除的实体。如果实体应该被软删除,则必须实现ISoftDelete接口,该接口仅定义IsDeleted属性。

Ex :

public class Person : Entity, ISoftDelete
{
    public virtual string Name { get; set; }

    public virtual bool IsDeleted { get; set; }
}

Here is the Link : ISoftDelete

这是Link:ISoftDelete

Source Code on Github

Github上的源代码

ASP.NET Boilerplate Documentation

ASP.NET Boilerplate文档