在 Asp.Net 动态数据中禁用一张表的删除但允许插入

发布于 2024-09-12 04:11:44 字数 81 浏览 5 评论 0原文

您好,我有一个使用用户表的网站。我希望能够添加用户但不能删除它们 - 我需要更新数据库中的一个文件,该文件表示“已删除” - 知道如何执行此操作吗?

Hi I have a site where I use a User Table. I want to be able to add users but not delete them - I need to update a filed in the database that said Is Deleted - Any Idea how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生殊途 2024-09-19 04:11:44

我建议使用 Linq to SQL 或实体框架(或其他 ORM)之类的东西,并创建一个 CRUD 存储库(创建、读取、更新、删除)。但“D”的方法实际上是更新 isDeleted 字段的更新方法。

这是我的 CRUD 存储库的示例(也带有服务层)。
http:// dotnetblogger.com/post/2010/07/04/MVC2-Separating-Concerns-with-Service-and-Repository-Layers.aspx

示例:

Public Class UserRepository : Implements IUserRepository 
    Private dc As MyDatabaseDataContext 
    Public Sub New() 
        dc = New MyDatabaseDataContext 
    End Sub 

    ''# note, this adds a user based on an OpenID - might not be what you're looking for
    Public Sub AddUser(ByVal openid As OpenID) Implements IUserRepository.AddUser 
        Dim user As New User 
        user.MemberSince = DateTime.Now 
        openid.User = user 

        dc.OpenIDs.InsertOnSubmit(openid) 
    End Sub 

    ''# This gets users as Queryable so that we can defer the query till the end.
    Public Function GetUsers() As IQueryable(Of User) Implements IUserRepository.GetUsers 
        Dim users = (From u In dc.Users 
                    Select u) 
        Return users.AsQueryable 
    End Function 

    ''# Here's the magic function that you're looking for. DELETE is actually an UPDATE.
    Public Sub DeleteUser(ByVal user as User) Implements IUserRepository.DeleteUser
        Dim _user = (From u In dc.Users 
            Where u.ID = user.ID 
            Select u).Single 

        _user.isDeleted = True
    End Sub


    ''# Basic Update Method
    Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser 
        Dim _user = (From u In dc.Users 
            Where u.ID = user.ID 
            Select u).Single 

        With _user 
            .About = user.About 
            .BirthDate = user.BirthDate 
            .Email = user.Email 
            .isClosed = user.isClosed 
            .isProfileComplete = user.isProfileComplete 
            .RegionID = user.RegionID 
            .Reputation = user.Reputation 
            .UserName = user.UserName 
            .WebSite = user.WebSite 
        End With 

    End Sub 

    ''# Make sure to call SubmitChanges
    Public Sub SubmitChanges() Implements IUserRepository.SubmitChanges 
        dc.SubmitChanges() 
    End Sub 
End Class 

I would suggest using something like Linq to SQL or Entity Framework (or other ORM), and create a CRUD repository (Create, Read, Update, Delete). But the method for the "D" will actually be an update method that updates the isDeleted field.

Here's an example of my CRUD repository (with a service layer as well).
http://dotnetblogger.com/post/2010/07/04/MVC2-Separating-Concerns-with-Service-and-Repository-Layers.aspx

Example:

Public Class UserRepository : Implements IUserRepository 
    Private dc As MyDatabaseDataContext 
    Public Sub New() 
        dc = New MyDatabaseDataContext 
    End Sub 

    ''# note, this adds a user based on an OpenID - might not be what you're looking for
    Public Sub AddUser(ByVal openid As OpenID) Implements IUserRepository.AddUser 
        Dim user As New User 
        user.MemberSince = DateTime.Now 
        openid.User = user 

        dc.OpenIDs.InsertOnSubmit(openid) 
    End Sub 

    ''# This gets users as Queryable so that we can defer the query till the end.
    Public Function GetUsers() As IQueryable(Of User) Implements IUserRepository.GetUsers 
        Dim users = (From u In dc.Users 
                    Select u) 
        Return users.AsQueryable 
    End Function 

    ''# Here's the magic function that you're looking for. DELETE is actually an UPDATE.
    Public Sub DeleteUser(ByVal user as User) Implements IUserRepository.DeleteUser
        Dim _user = (From u In dc.Users 
            Where u.ID = user.ID 
            Select u).Single 

        _user.isDeleted = True
    End Sub


    ''# Basic Update Method
    Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser 
        Dim _user = (From u In dc.Users 
            Where u.ID = user.ID 
            Select u).Single 

        With _user 
            .About = user.About 
            .BirthDate = user.BirthDate 
            .Email = user.Email 
            .isClosed = user.isClosed 
            .isProfileComplete = user.isProfileComplete 
            .RegionID = user.RegionID 
            .Reputation = user.Reputation 
            .UserName = user.UserName 
            .WebSite = user.WebSite 
        End With 

    End Sub 

    ''# Make sure to call SubmitChanges
    Public Sub SubmitChanges() Implements IUserRepository.SubmitChanges 
        dc.SubmitChanges() 
    End Sub 
End Class 
甜中书 2024-09-19 04:11:44

我为每个实体创建一个自定义表单。

I create an custom form for every Entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文