RelationalQueryableExtensions.FromSql 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| FromSql<TEntity>(DbSet<TEntity>, FormattableString) |
根據一個插值字串(代表 SQL 查詢)建立一個 LINQ 查詢。 |
| FromSql<TEntity>(IQueryable<TEntity>, FormattableString) |
已淘汰.
根據一個插值字串(代表 SQL 查詢)建立一個 LINQ 查詢。
如果資料庫提供者支援在提供的 SQL 上撰寫,你可以使用 LINQ 運算子 -
和任何接受 SQL 的 API 一樣,必須對使用者輸入進行參數化,以防止 SQL 注入攻擊。 你可以在 SQL 查詢字串中加入插值參數的佔位符。 你提供的任何插值參數值都會自動轉換成 DbParameter - |
| FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[]) |
已淘汰.
根據原始 SQL 查詢建立 LINQ 查詢。
如果資料庫提供者支援在提供的 SQL 上撰寫,你可以使用 LINQ 運算子 -
和任何接受 SQL 的 API 一樣,必須對使用者輸入進行參數化,以防止 SQL 注入攻擊。 你可以在 SQL 查詢字串中加入參數佔位符,然後以參數值作為額外參數。 你提供的任何參數值都會自動轉換成 DbParameter -
此超載同時接受 DbParameter 實例作為參數值。 這讓你可以在 SQL 查詢字串中使用命名參數—— |
| FromSql<TEntity>(IQueryable<TEntity>, String, Object[]) |
根據原始 SQL 查詢建立 LINQ 查詢。
如果資料庫提供者支援在提供的 SQL 上撰寫,你可以使用 LINQ 運算子 -
和任何接受 SQL 的 API 一樣,必須對使用者輸入進行參數化,以防止 SQL 注入攻擊。 你可以在 SQL 查詢字串中加入參數佔位符,然後以參數值作為額外參數。 你提供的任何參數值都會自動轉換成 DbParameter -
你也可以建構一個 DbParameter,並以參數值提供給 。 這讓你可以在 SQL 查詢字串中使用命名參數—— |
FromSql<TEntity>(DbSet<TEntity>, FormattableString)
根據一個插值字串(代表 SQL 查詢)建立一個 LINQ 查詢。
public static System.Linq.IQueryable<TEntity> FromSql<TEntity>(this Microsoft.EntityFrameworkCore.DbSet<TEntity> source, FormattableString sql) where TEntity : class;
static member FromSql : Microsoft.EntityFrameworkCore.DbSet<'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 DbSet(Of TEntity), sql As FormattableString) As IQueryable(Of TEntity)
類型參數
- TEntity
元素 source的類型。
參數
- source
- DbSet<TEntity>
用 an IQueryable<T> 作為插值字串 SQL 查詢的基底(通常是 DbSet<TEntity>。
插值字串代表帶有參數的 SQL 查詢。
傳回
表示 IQueryable<T> 插值字串 SQL 查詢。
備註
如果資料庫提供者支援在提供的 SQL 上編寫,你可以使用 LINQ 運算子在原始 SQL 查詢上編寫。
和任何接受 SQL 的 API 一樣,必須對使用者輸入進行參數化,以防止 SQL 注入攻擊。 你可以在 SQL 查詢字串中加入插值參數的佔位符。 你提供的任何插值參數值都會自動轉換成 DbParameter。
更多資訊與範例請參閱「 使用 EF Core 執行原始 SQL 指令 」。
適用於
FromSql<TEntity>(IQueryable<TEntity>, FormattableString)
警告
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.
根據一個插值字串(代表 SQL 查詢)建立一個 LINQ 查詢。
如果資料庫提供者支援在提供的 SQL 上撰寫,你可以使用 LINQ 運算子 - context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)在原始 SQL 查詢上進行組合。
和任何接受 SQL 的 API 一樣,必須對使用者輸入進行參數化,以防止 SQL 注入攻擊。 你可以在 SQL 查詢字串中加入插值參數的佔位符。 你提供的任何插值參數值都會自動轉換成 DbParameter - context.Blogs.FromSql($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})")。
public static System.Linq.IQueryable<TEntity> FromSql<TEntity>(this System.Linq.IQueryable<TEntity> source, FormattableString sql) where TEntity : class;
[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)
[<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)>]
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)
類型參數
- TEntity
元素 source的類型。
參數
- source
- IQueryable<TEntity>
用 an IQueryable<T> 作為插值字串 SQL 查詢的基底(通常是 DbSet<TEntity>。
代表 SQL 查詢的插值字串。
傳回
表示 IQueryable<T> 插值字串 SQL 查詢。
- 屬性
適用於
FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[])
警告
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.
根據原始 SQL 查詢建立 LINQ 查詢。
如果資料庫提供者支援在提供的 SQL 上撰寫,你可以使用 LINQ 運算子 - context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)在原始 SQL 查詢上進行組合。
和任何接受 SQL 的 API 一樣,必須對使用者輸入進行參數化,以防止 SQL 注入攻擊。 你可以在 SQL 查詢字串中加入參數佔位符,然後以參數值作為額外參數。 你提供的任何參數值都會自動轉換成 DbParameter - context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)。
此超載同時接受 DbParameter 實例作為參數值。 這讓你可以在 SQL 查詢字串中使用命名參數—— 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, Microsoft.EntityFrameworkCore.RawSqlString sql, params object[] parameters) where TEntity : class;
[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)
[<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)>]
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)
類型參數
- TEntity
元素 source的類型。
參數
- source
- IQueryable<TEntity>
用 an IQueryable<T> 作為原始 SQL 查詢的基礎(通常是 DbSet<TEntity>)。
- sql
- RawSqlString
原始的 SQL 查詢。 注意。 字串文字可以在這裡傳遞,因為 RawSqlString 是隱含可轉換為字串。
- parameters
- Object[]
參數要分配的值。
傳回
表示 IQueryable<T> 原始的 SQL 查詢。
- 屬性
適用於
FromSql<TEntity>(IQueryable<TEntity>, String, Object[])
根據原始 SQL 查詢建立 LINQ 查詢。
如果資料庫提供者支援在提供的 SQL 上撰寫,你可以使用 LINQ 運算子 - context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name)在原始 SQL 查詢上進行組合。
和任何接受 SQL 的 API 一樣,必須對使用者輸入進行參數化,以防止 SQL 注入攻擊。 你可以在 SQL 查詢字串中加入參數佔位符,然後以參數值作為額外參數。 你提供的任何參數值都會自動轉換成 DbParameter - context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm)。
你也可以建構一個 DbParameter,並以參數值提供給 。 這讓你可以在 SQL 查詢字串中使用命名參數—— 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)
類型參數
- TEntity
元素 source的類型。
參數
- source
- IQueryable<TEntity>
用 an IQueryable<T> 作為原始 SQL 查詢的基礎(通常是 DbSet<TEntity>)。
- sql
- String
原始的 SQL 查詢。
- parameters
- Object[]
參數要分配的值。
傳回
表示 IQueryable<T> 原始的 SQL 查詢。