Execute Raw SQL Query with “FromSqlRaw()” method

Consider a Student entity:

public class Student

{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int Standard { get; set; }
    public string Address { get; set; }
}

To get details of all students that are in Standard 10 you can execute an SQL query using FromSqlRaw() method like this:

var context = new SchoolContext();
var students = context.Student.FromSqlRaw("Select * from Student where Standard = 10").ToList();

Here your raw query – Select * from Student where Standard = 10 will be executed on the database and will give a list of all students that are in ‘standard 10’.

Entity Framework Core
SQL

Comments

Leave a Comment

All fields are required. Your email address will not be published.