Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DbSet.SqlQuery Method (String, Object[])

 

Creates a raw SQL query that will return entities in this set. By default, the entities returned are tracked by the context; this can be changed by calling AsNoTracking on the DbRawSqlQuery returned. Note that the entities returned are always of the type for this set and never of a derived type. If the table or tables queried may contain data for other entity types, then the SQL query must be written appropriately to ensure that only entities of the correct type are returned. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Set(typeof(Blog)).SqlQuery("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Set(typeof(Blog)).SqlQuery("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

Namespace:   System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)

abstract SqlQuery : 
        sql:string *
        [<ParamArrayAttribute>] parameters:Object[] -> DbSqlQuery
override SqlQuery : 
        sql:string *
        [<ParamArrayAttribute>] parameters:Object[] -> DbSqlQuery

Parameters

sql
Type: System.String

The SQL query string.

parameters
Type: System.Object[]

The parameters to apply to the SQL query string. If output parameters are used, their values will not be available until the results have been read completely. This is due to the underlying behavior of DbDataReader, see http://go.microsoft.com/fwlink/?LinkID=398589 for more details.

Return Value

Type: System.Data.Entity.Infrastructure.DbSqlQuery

A DbSqlQuery object that will execute the query when it is enumerated.

Return to top
Show:
© 2015 Microsoft