DbSet<TEntity>.SqlQuery(String, Object[]) DbSet<TEntity>.SqlQuery(String, Object[]) DbSet<TEntity>.SqlQuery(String, Object[]) Method

Definition

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 DbSqlQuery<TEntity> 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.Blogs.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.Blogs.SqlQuery("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

public virtual System.Data.Entity.Infrastructure.DbSqlQuery<TEntity> SqlQuery (string sql, params object[] parameters);
abstract member SqlQuery : string * obj[] -> System.Data.Entity.Infrastructure.DbSqlQuery<'Entity (requires 'Entity : null)>
override this.SqlQuery : string * obj[] -> System.Data.Entity.Infrastructure.DbSqlQuery<'Entity (requires 'Entity : null)>
Public Overridable Function SqlQuery (sql As String, ParamArray parameters As Object()) As DbSqlQuery(Of TEntity)

Parameters

sql
String String String

The SQL query string.

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

Returns

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

Applies to