Question

I have a project with a formidable data access layer using LinqtoSQL for just about anything touching our databases. I needed to build a helper class that bridges some common crud operations from CLSA objects to LinqToSql ones. Everything has been operating swimmingly until I needed to do a truncate on a table and all I had were “delete” methods.

Uh-oh. A quick search reveals that some people are using YourContext.ExecuteCommand(), which is nice and all, but I am trying to go “t-sql-less” as much as possible these days.

Is there a LINQ way to perform a truncate on a table? Or am I just clueless?

Was it helpful?

Solution

This is not possible without doing a custom T-SQL query. Doing a .Delete() and SubmitChanges afterwords would, as you probably already know, result in a DELETE statement.

Of course you could create a stored procedure that truncates the table, and then call the procedure from LINQ, but that isn't really what you're looking for I believe.

OTHER TIPS

You can do something like this:

yourDataContext.ExecuteCommand("TRUNCATE TABLE YourTable");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top