I have the following statement that is failing in a C# program using LINQ to Entity Framework 4:
int top = 1000;
string name = "StagingTable";
using (var context = CreateObjectContext())
{
int count = context.ExecuteStoreCommand(string.Concat("DELETE TOP {0} FROM ", name), top);
}
The context is created correctly (used in other parts of the program) and the table name is spelled correctly. According to Microsoft documentation, this should work to delete a maximum number of records from the table, but instead throws an exception:
System.Data.SqlClient.SqlException: Incorrect syntax near @p0.
I checked and rechecked the syntax of the ExecuteStoreCommand
and could not find anything wrong.
How is it possible to use the TOP
clause in a DELETE
statement like this?