In this article I will explain with an example, what is the difference between FromSql and ExecuteSqlCommand functions in Entity Framework Core 2.1 framework.
Note: For beginners in ASP.Net Core MVC and Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Entity Framework Core 2.1: Difference between FromSql and ExecuteSqlCommand
 
I have already inserted few records in the table.
Entity Framework Core 2.1: Difference between FromSql and ExecuteSqlCommand
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Differences between FromSql and ExecuteSqlCommand
FromSql
1. The FromSql function is used to execute SQL queries or Stored Procedures that fetch data. Example SELECT queries.
2. Returns the rows from Database Table as IQueryable collection.
3. It is available in the DbSet collection.
 
ExecuteSqlCommand
1. The ExecuteSqlCommand is used to execute SQL queries or Stored Procedures that does not fetch data. Example INSERT, UPDATE and DELETE queries.
2. Returns the count of the number of Rows affected by the operation in INTEGER format.
3. It is available in the Database property of the DbContext class.
 
 
FromSql Example
The following DbContext class has a method named GetCustomer which accepts the parameter CustomerId.
Inside the function, the SQL Query to fetch the record from the Customers table is executed using the FromSql function.
The FromSql function accepts the following two parameters:
1. SQL Query – SQL Query to be executed.
2. Parameters – One or more objects of SqlParameter class.
Note: The FromSql function belongs to the Microsoft.EntityFrameworkCore namespace.
 
using System.Linq;
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
 
namespace EF_Customers_MVC_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
 
        public IQueryable<Customer> GetCustomer(int customerId)
        {
            string sql = @"SELECT * FROM Customers WHERE CustomerId = @CustomerId";
            SqlParameter pCustomerId = new SqlParameter("@CustomerId", customerId);
            return this.Customers.FromSql(sql, pCustomerId);
        }
    }
}
 
 
ExecuteSqlCommand Example
The following DbContext class has a method named DeleteCustomer which accepts the parameter CustomerId.
Inside the function, the SQL Query to delete the record from the Customers table is executed using the ExecuteSqlCommand function.
The ExecuteSqlCommand function accepts the following two parameters:
1. SQL Query – SQL Query to be executed.
2. Parameters – One or more objects of SqlParameter class.
Note: The ExecuteSqlCommand function belongs to the Microsoft.EntityFrameworkCore namespace.
 
using System.Linq;
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
 
namespace EF_Customers_MVC_Core
{
    publicclass DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
 
        public void DeleteCustomer(int customerId)
        {
            string sql = @"DELETE FROM Customers WHERE CustomerId = @CustomerId";
            SqlParameter pCustomerId = new SqlParameter("@CustomerId", customerId);
            int affected = this.Database.ExecuteSqlCommand(sql, pCustomerId);
        }
    }
}