The ObjectContext
exposes a set of methods that allow you to execute SQL directly on your server and (depending on the method you choose and the SQL you execute) materialize them as entities in your context.
The ObjectContext.ExecuteStoreCommand()
method can be used to execute any SQL that’s valid on your database server. It isn’t required, but it is considered best practice to use parameters rather than literal values:
The DbContext
also supports executing SQL directly, but it does it by way of the context’s Database
property and the ExecuteSqlCommand()
method. The syntax for specifying the SQL and any parameters is identical:
Dim sqlStr As String = "INSERT INTO Recipes (Name) VALUES ({0})"
MyDbContext.ExecuteStoreCommand(sqlStr, "Denver Omelet");