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?
A:
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
rockinthesixstring
2010-08-02 18:15:50
How do I set that the delete function in Dynamic Data use my code?
Jedi Master Spooky
2010-08-02 19:36:59
Dynamic Data is still using Linq To SQL. Go into the code behind of the Edit template and post the edit code (just edit your existing question). I don't use Dynamic Data (I've chosen MVC instead), but the principles will be the same.
rockinthesixstring
2010-08-02 20:56:50