Entity Framework Core allows you to drop down to raw SQL queries when working with a relational database. This can be useful if the query you want to perform can't be expressed using LINQ, or if using a LINQ query is resulting in inefficient SQL being sent to the database. Raw SQL queries can return entity types or, starting with EF Core 2.1, query types that are part of your model.

Tip
You can view this article's sample on GitHub.

Limitations

There are a few limitations to be aware of when using raw SQL queries:

  • The SQL query must return data for all properties of the entity or query type.
  • The column names in the result set must match the column names that properties are mapped to. Note this is different from EF6 where property/column mapping was ignored for raw SQL queries and result set column names had to match the property names.
  • The SQL query cannot contain related data. However, in many cases you can compose on top of the query using the Include operator to return related data (see Including related data).
  • SELECT statements passed to this method should generally be composable: If EF Core needs to evaluate additional query operators on the server (for example, to translate LINQ operators applied after FromSql), the supplied SQL will be treated as a subquery. This means that the SQL passed should not contain any characters or options that are not valid on a subquery, such as:
    • a trailing semicolon
    • On SQL Server, a trailing query-level hint (for example, OPTION (HASH JOIN))
    • On SQL Server, an ORDER BY clause that is not accompanied of TOP 100 PERCENT in the SELECT clause
  • SQL statements other than SELECT are recognized automatically as non-composable. As a consequence, the full results of stored procedures are always returned to the client and any LINQ operators applied after FromSql are evaluated in-memory.

Basic raw SQL queries

You can use the FromSql extension method to begin a LINQ query based on a raw SQL query

var blogs = context.Blogs
.FromSql("SELECT * FROM dbo.Blogs")
.ToList();

Raw SQL queries can be used to execute a stored procedure.

var blogs = context.Blogs
.FromSql("EXECUTE dbo.GetMostPopularBlogs")
.ToList();

Use SqlParameter instance to specify the value of IN or OUT parameters to execute a stored procedure as below:

int totalRowCount = default(int);

var paramLanguageCode = new SqlParameter("@languageCode", languageCode);
var paramCurrentPage = new SqlParameter("@currentPage", currentPage);
var paramPageSize = new SqlParameter("@pageSize", pageSize);
var paramOutTotalRowCount = new SqlParameter("@totalRowCount", totalRowCount)
{
Direction = ParameterDirection.Output
}; //var paramOutTotalRowCount = new SqlParameter()
//{
// ParameterName = "@totalRowCount",
// Direction = ParameterDirection.Output,
// SqlDbType = SqlDbType.Int
//}; //var parameterQuestionaryCode = new SqlParameter()
//{
// ParameterName = "@questionaryCode",
// SqlDbType = SqlDbType.NVarChar,
// Direction = ParameterDirection.Output,
// Size = 50 //注意如果是SqlDbType.NVarChar的Output参数,记得还要定义Size的大小,否者执行的时候会报错
//}; var categoriesView = context.SP_GetCategoriesViewInPage.FromSql("EXEC [MD].[SP_GetCategoriesViewInPage] @languageCode, @currentPage, @pageSize, @totalRowCount OUT", paramLanguageCode, paramCurrentPage, paramPageSize, paramOutTotalRowCount).ToList();
totalRowCount = Convert.ToInt32(paramOutTotalRowCount.Value);

同样INOUT参数也可以用于执行DbContext.Database.ExecuteSqlCommand方法,来返回存储过程的OUT参数值,如下所示:

int totalRowCount = default(int);

var paramLanguageCode = new SqlParameter("@languageCode", languageCode);
var paramCurrentPage = new SqlParameter("@currentPage", currentPage);
var paramPageSize = new SqlParameter("@pageSize", pageSize);
var paramOutTotalRowCount = new SqlParameter("@totalRowCount", totalRowCount)
{
Direction = ParameterDirection.Output
}; //var paramOutTotalRowCount = new SqlParameter()
//{
// ParameterName = "@totalRowCount",
// Direction = ParameterDirection.Output,
// SqlDbType = SqlDbType.Int
//}; //var parameterQuestionaryCode = new SqlParameter()
//{
// ParameterName = "@questionaryCode",
// SqlDbType = SqlDbType.NVarChar,
// Direction = ParameterDirection.Output,
// Size = 50 //注意如果是SqlDbType.NVarChar的Output参数,记得还要定义Size的大小,否者执行的时候会报错
//}; context.Database.ExecuteSqlCommand("EXEC [MD].[SP_GetCategoriesViewInPage] @languageCode, @currentPage, @pageSize, @totalRowCount OUT", paramLanguageCode, paramCurrentPage, paramPageSize, paramOutTotalRowCount);
totalRowCount = Convert.ToInt32(paramOutTotalRowCount.Value);

下面代码展示了如何创建一个SQL Server数据库DECIMAL(8,4)类型的SqlParameter参数@Price

private static void AddSqlParameter(SqlCommand command)
{
SqlParameter parameter = new SqlParameter("@Price", SqlDbType.Decimal);
parameter.Value = 3.1416;
parameter.Precision = ;
parameter.Scale = ; command.Parameters.Add(parameter);
}

更多关于SqlParameter的信息,请参阅"SqlParameter Class"

Passing parameters

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 placeholders 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.
The following example passes a single parameter to a stored procedure. While this may look like String.Format syntax, the supplied value is wrapped in a parameter and the generated parameter name inserted where the {0} placeholder was specified.

var user = "johndoe";

var blogs = context.Blogs
.FromSql("EXECUTE dbo.GetMostPopularBlogsForUser {0}", user)
.ToList();

This is the same query but using string interpolation syntax, which is supported in EF Core 2.0 and above:

var user = "johndoe";

var blogs = context.Blogs
.FromSql($"EXECUTE dbo.GetMostPopularBlogsForUser {user}")
.ToList();

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

var user = new SqlParameter("user", "johndoe");

var blogs = context.Blogs
.FromSql("EXECUTE dbo.GetMostPopularBlogsForUser @user", user)
.ToList();

Composing with LINQ

If the SQL query can be composed on in the database, then you can compose on top of the initial raw SQL query using LINQ operators. SQL queries that can be composed on being with the SELECT keyword.
The following example uses a raw SQL query that selects from a Table-Valued Function (TVF) and then composes on it using LINQ to perform filtering and sorting.

var searchTerm = ".NET";

var blogs = context.Blogs
.FromSql($"SELECT * FROM dbo.SearchBlogs({searchTerm})")
.Where(b => b.Rating > )
.OrderByDescending(b => b.Rating)
.ToList();

Including related data

Composing with LINQ operators can be used to include related data in the query.

var searchTerm = ".NET";

var blogs = context.Blogs
.FromSql($"SELECT * FROM dbo.SearchBlogs({searchTerm})")
.Include(b => b.Posts)
.ToList();
Warning
Always use parameterization for raw SQL queries: APIs that accept a raw SQL string such as FromSql and ExecuteSqlCommand allow values to be easily passed as parameters. In addition to validating user input, always use parameterization for any values used in a raw SQL query/command. If you are using string concatenation to dynamically build any part of the query string then you are responsible for validating any input to protect against SQL injection attacks.

EF Core 3.0更新
注意在EF Core 3.0中,FromSql方法和ExecuteSqlCommand方法都已经过时,请使用FromSqlRaw方法和ExecuteSqlRaw方法进行替代

参考文献:

Working with Stored Procedure in Entity Framework Core

原文链接

EF Core 2.1 Raw SQL Queries (转自MSDN)的更多相关文章

  1. EF: Raw SQL Queries

    Raw SQL Queries Entity Framework allows you to query using LINQ with your entity classes. However, t ...

  2. Executing Raw SQL Queries using Entity Framework

    原文 Executing Raw SQL Queries using Entity Framework While working with Entity Framework developers m ...

  3. 在.NET Core类库中使用EF Core迁移数据库到SQL Server

    前言 如果大家刚使用EntityFramework Core作为ORM框架的话,想必都会遇到数据库迁移的一些问题. 起初我是在ASP.NET Core的Web项目中进行的,但后来发现放在此处并不是很合 ...

  4. EF core 学习 执行原生sql语句 之ExecuteReader 和ExecuteScalar

    通过ef core 源码分析 Microsoft.EntityFrameworkCore.Storage.RelationalCommandExtensions类中有相应的方法 为此得到相应的结果: ...

  5. EF Core中,通过实体类向SQL Server数据库表中插入数据后,实体对象是如何得到数据库表中的默认值的

    我们使用EF Core的实体类向SQL Server数据库表中插入数据后,如果数据库表中有自增列或默认值列,那么EF Core的实体对象也会返回插入到数据库表中的默认值. 下面我们通过例子来展示,EF ...

  6. EntityFramework Core技术线路(EF7已经更名为EF Core,并于2016年6月底发布)

    官方文档英文地址:https://github.com/aspnet/EntityFramework/wiki/Roadmap 历经延期和更名,新版本的实体框架终于要和大家见面了,虽然还有点害羞.请大 ...

  7. [转]How to get return values and output values from a stored procedure with EF Core?

    本文转自:https://stackoverflow.com/questions/43935345/how-to-get-return-values-and-output-values-from-a- ...

  8. [转]EntityFramework Core技术线路(EF7已经更名为EF Core,并于2016年6月底发布)

    本文转自:http://www.cnblogs.com/VolcanoCloud/p/5572408.html 官方文档英文地址:https://github.com/aspnet/EntityFra ...

  9. EF core 性能调优

    Entity Framework Core performance tuning – a worked example Last Updated: February 25, 2019 | Create ...

随机推荐

  1. 事件处理程序DOM0,DOM2,IE的区别总结

    一.事件流   顺序 备注 事件冒泡 目标对象~document对象   事件捕获 document对象~目标对象 老版本浏览器不支持 DOM事件流 document对象~目标对象~document对 ...

  2. AngularJS+RequireJs实现动态加载JS和页面的方案研究【下】

    about.js: [html] view plain copy 在CODE上查看代码片派生到我的代码片 define(['app'], function(app) { app.controller( ...

  3. Java设计模式—享元模式

    享元模式:是池技术的重要实现方式. 定义如下: 使用共享对象可有效地支持大量的细粒度的对象. 个人理解:享元模式利用共享对象的技术,解决了Java中内存溢出的问题. 享元模式的定义为我们提出了两个要求 ...

  4. java中静态代码块的用法和static用法(转)

    (一)java 静态代码块 静态方法区别 一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程 ...

  5. slice()方法 和splice 方法的区别

    定义 splice() 方法 用于插入.删除或替换数组的元素. slice() 方法 可提取字符串的某个部分,并以新的字符串返回被提取的部分. 更多的可查看: http://www.cnblogs.c ...

  6. Microsoft Toolkit.exe激活office 2010方法

    1.双击打开激活工具 2.点击下方的office图标. 3.选择Activation标签,下拉选择AutoKMS,点击Install,完成后点击Activate,即可.

  7. sql处理百万级以上的数据提高查询速度的方法

    原文:http://blog.csdn.net/zhengyiluan/article/details/51671599 处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中 ...

  8. Java基础之PDF文件的合并

    1.首先下载一个jar包:pdfbox-app-1.7.1.jar 2.代码如下: package com; import java.io.File; import java.io.IOExcepti ...

  9. 初识WCF1

    参照: http://blog.csdn.net/songyefei/article/details/7363296 第一篇 Hello WCF WCF(Windows Communication F ...

  10. Django基础之form操作

    Form表单的功能 自动生成HTML表单元素 检查表单数据的合法性 如果验证错误,重新显示表单(数据不会重置) 数据类型转换(字符类型的数据转换成相应的Python类型) Form相关的对象包括 Wi ...