Entity Framework Core Series Tutorial-23-Native SQL Query

Execute native SQL query in Entity Framework Core

Entity Framework Core provides the DbSet.FromSql () method to perform raw SQL queries on the underlying database and obtain the results as entity objects.
The following example shows how to execute a raw SQL query against the MS SQL Server database.

var context = new SchoolContext();
var students = context.Students
                  .FromSql("Select * from Students where Name = 'Bill'")
                  .ToList();

In the above example, the FromSql () method is used after the Student entity set (DbSet <Student>), so the specified SQL query must return a record from the Student table, which will be converted in the Student entity. Entity Framework Core will execute the specified query on the database, that is, in the above example, select * from Student with * Name = 'Bill'.

Parameterized Query

The FromSql method allows parameterized queries using the string interpolation syntax in C #, as shown below.

string name = "Bill";
var context = new SchoolContext();
var students = context.Students
                    .FromSql($"Select * from Students where Name = '{name}'")
                    .ToList();

The following is also valid.
string name = "Bill";
var context = new SchoolContext();
var students = context.Students
                    .FromSql("Select * from Students where Name = '{0}'", name)
                    .ToList();

The above example will execute the following SQL query on the SQL Server database:

exec sp_executesql N'Select * from Students where Name = ''@p0''
',N'@p0 nvarchar(4000)',@p0=N'Bill'
go

LINQ operator

You can also use the LINQ operator after the original query using the FromSql method.

string name = "Bill";
var context = new SchoolContext();
var students = context.Students
                    .FromSql("Select * from Students where Name = '{0}'", name)
                    .OrderBy(s => s.StudentId)
                    .ToList();

In the above example, EF Core performs the following query by combining the FromSql method and the OrderBy operator.

exec sp_executesql N'SELECT [s].[StudentId], [s].[Name]
FROM (
    Select * from Students where Name = ''@p0''
) AS [s]
ORDER BY [s].[StudentId]',N'@p0 nvarchar(4000)',@p0=N'Bill'
go

Limitations of FromSql

The SQL query must return entities of the same type as DbSet <T>. For example, if you use FromSql after a student, the specified query cannot return the course entity. The temporary type returned from the FromSql () method is in the to-do list.
The SQL query must return all columns of the table. For example, context.Students.FromSql ("Select StudentId, LastName from students). ToList () will throw an exception.
SQL query cannot include JOIN query to get related data. After FromSql () method, use Include method to load related entities.

Published 177 original articles · 61 praises · 170,000 views

Guess you like

Origin blog.csdn.net/xingkongtianyuzhao/article/details/104227957