If there are a handful of tables and the data belonging to the user isn't much then you could issue the commands in LINQ to SQL. By "isn't much" I mean a few records that barely change, such as login info, address info, and preferences. Ideally you would want to keep all this logic together to ensure the steps are always executed in the intended order. Even if this is the case, a stored procedure may still be used.
On the other hand, if the data is expected to be a lot, such as a forum user who has multiple posts, comments, favorites, bookmarks, etc. then I recommend using a stored procedure that takes the user's ID and does the rest as needed. The concern is that a single delete statement is issued for each matching record. That is what causes poorer performance than a delete statement that deletes all records associated with the user ID in one statement.
Option #1: Use a Stored Procedure
You can map the stored procedure to your DataContext, which gives you the ability to use it as dc.DeleteUserAccount(userId)
. Scott Gu has an excellent blog post that can get you started: LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures).
Option #2: Use Terry Aney's Batch Update Code
Terry Aney has a great blog post that discusses his experience developing the ability to batch update and delete with LINQ to SQL in an efficient manner. Instead of generating a single update/delete statement for each matching record, the code will generate a single statement for all records the way we would normally write them. The post and code can be found here: Batch Updates and Deletes with LINQ to SQL.
Option #3: Use the DataContext.ExecuteCommand Method
The DataContext.ExecuteCommand method can be used to execute SQL directly, such as:
int affectedRecords =
dc.ExecuteCommand("Update Person SET FirstName = {0} WHERE FirstName = {1}", "Foo", "Bar");
Notice the use of {0}
and {1}
which allow input to be parametrized. Use this instead of concatenating to prevent SQL injection attacks.
If you're going to use this then you might as well go with option #1.