What is the correct LINQtoSQL-ish way to truncate a table?

I have a project with a huge level of data access using LinqtoSQL for almost anything related to our databases. I needed to create a helper class that combines some common crud operations from CLSA objects in LinqToSql. Everything works smoothly until I need to truncate on the table, and all I had were the removal methods.

Oooh A quick search reveals that some people use YourContext.ExecuteCommand (), which is nice and all, but I try as much "t-sql-less" these days.

Is there a LINQ way to crop a table ? Or am I just clueless ?

+3
source share
2 answers

This is not possible without executing a custom T-SQL query. Executing the .Delete () and SubmitChanges sequences after you probably already know will result in a DELETE declaration.

Of course, you can create a stored procedure that truncates the table and then call the procedure from LINQ, but this is actually not what you are looking for.

+3
source

You can do something like this:

yourDataContext.ExecuteCommand("TRUNCATE TABLE YourTable");
+2
source

Source: https://habr.com/ru/post/1698119/


All Articles