Executing SQL directly

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:

Image

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: ...

Get Fluent Entity Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.