RelationalQueryableExtensions.FromSql Method

Definition

Overloads

FromSql<TEntity>(IQueryable<TEntity>, FormattableString)

Creates a LINQ query based on an interpolated string representing a SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
.

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 interpolated parameter place holders in the SQL query string. Any interpolated parameter values you supply will automatically be converted to a DbParameter -

context.Blogs.FromSql($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})")
.

FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[])

Creates a LINQ query based on a raw SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
.

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.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)
.

This overload also accepts DbParameter instances as parameter values. This allows you to use named parameters in the SQL query string -

context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

FromSql<TEntity>(IQueryable<TEntity>, String, Object[])

Creates a LINQ query based on a raw SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
.

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.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)
.

You can also construct a DbParameter and supply it to as a parameter value. This allows you to use named parameters in the SQL query string -

context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

FromSql<TEntity>(IQueryable<TEntity>, FormattableString)

Warning

This API is now obsolete.

Creates a LINQ query based on an interpolated string representing a SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
.

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 interpolated parameter place holders in the SQL query string. Any interpolated parameter values you supply will automatically be converted to a DbParameter -

context.Blogs.FromSql($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})")
.

[System.Obsolete("For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.", true)]
public static System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, FormattableString sql) where TEntity : class;
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * FormattableString -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function FromSql(Of TEntity As Class) (source As IQueryable(Of TEntity), sql As FormattableString) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of the elements of source.

Parameters

source
IQueryable<TEntity>

An IQueryable<T> to use as the base of the interpolated string SQL query (typically a DbSet<TEntity>).

sql
FormattableString

The interpolated string representing a SQL query.

Returns

IQueryable<TEntity>

An IQueryable<T> representing the interpolated string SQL query.

Attributes

FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[])

Warning

This API is now obsolete.

Creates a LINQ query based on a raw SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
.

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.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)
.

This overload also accepts DbParameter instances as parameter values. This allows you to use named parameters in the SQL query string -

context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

[System.Obsolete("For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query.", true)]
public static System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, Microsoft.EntityFrameworkCore.RawSqlString sql, params object[] parameters) where TEntity : class;
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * Microsoft.EntityFrameworkCore.RawSqlString * obj[] -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function FromSql(Of TEntity As Class) (source As IQueryable(Of TEntity), sql As RawSqlString, ParamArray parameters As Object()) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of the elements of source.

Parameters

source
IQueryable<TEntity>

An IQueryable<T> to use as the base of the raw SQL query (typically a DbSet<TEntity>).

sql
RawSqlString

The raw SQL query. NB. A string literal may be passed here because RawSqlString is implicitly convertible to string.

parameters
Object[]

The values to be assigned to parameters.

Returns

IQueryable<TEntity>

An IQueryable<T> representing the raw SQL query.

Attributes

FromSql<TEntity>(IQueryable<TEntity>, String, Object[])

Creates a LINQ query based on a raw SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators -

context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)
.

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.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)
.

You can also construct a DbParameter and supply it to as a parameter value. This allows you to use named parameters in the SQL query string -

context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

public static System.Linq.IQueryable<TEntity> FromSql<TEntity> (this System.Linq.IQueryable<TEntity> source, string sql, params object[] parameters) where TEntity : class;
static member FromSql : System.Linq.IQueryable<'Entity (requires 'Entity : null)> * string * obj[] -> System.Linq.IQueryable<'Entity (requires 'Entity : null)> (requires 'Entity : null)
<Extension()>
Public Function FromSql(Of TEntity As Class) (source As IQueryable(Of TEntity), sql As String, ParamArray parameters As Object()) As IQueryable(Of TEntity)

Type Parameters

TEntity

The type of the elements of source.

Parameters

source
IQueryable<TEntity>

An IQueryable<T> to use as the base of the raw SQL query (typically a DbSet<TEntity>).

sql
String

The raw SQL query.

parameters
Object[]

The values to be assigned to parameters.

Returns

IQueryable<TEntity>

An IQueryable<T> representing the raw SQL query.

Applies to