Mulesoft Developer
Mulesoft Developer

Reputation: 2824

Linq syntax getting error

I want to use truncate table command and searching for it and found solution like this

context.Entities.DeleteAllOnSubmit(dc.Entities);
and 
context.ExecuteCommand("DELETE FROM EntityName");
or
context.ExecuteCommand("TRUNCATE TABLE EntityName");

But i didn't get it these methods in LINQ. what is context here?

in sql we use this syntax

Truncate table tablename

Upvotes: 0

Views: 119

Answers (2)

Pleun
Pleun

Reputation: 8920

Well, using Linq-2-sql to throw raw sql commands at the database is possible but in this case linq-2-sql does not really give you any benefits. You may as well use plain Ado.Net in this case.

Anyway, If you have a real linq-2-sql datacontext it WILL show you the ExecuteCommand but you need to create your datacontext first.

Something like:

  using (notebookDataContext db = new notebookDataContext())
        { 
           db.ExecuteCommand( commandString, params);
         }

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503140

context would be an instance of the relevant database-specific class which extends DataContext. The LINQ to SQL designer will create that class for you.

I would strongly advise that you read a tutorial on LINQ to SQL before trying to write any code - it will explain the data context and its purpose so that you can use LINQ to SQL properly.

EDIT: Okay, you've now explained that you're using Entity Framework, not LINQ to SQL (as per your tags). That explains why you're not seeing the LINQ to SQL methods...

Upvotes: 1

Related Questions