Export (0) Print
Expand All

DbSet.SqlQuery Method

[This page is specific to the Entity Framework version 6. The latest version is available as the 'Entity Framework' NuGet package. For more information about Entity Framework, see msdn.com/data/ef.]

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)

'Declaration
Public Overridable Function SqlQuery ( _
	sql As String, _
	ParamArray parameters As Object() _
) As DbSqlQuery
'Usage
Dim instance As DbSet 
Dim sql As String 
Dim parameters As Object()
Dim returnValue As DbSqlQuery 

returnValue = instance.SqlQuery(sql, parameters)

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.
Show:
© 2014 Microsoft