.net core or MVC using entity framework execute Raw SQL Queries, select, insert, update and delete.

Execute Raw SQL Queries in Entity Framework.

In ado.net we know we directly apply queries in C# code, so in entity framework mostly we all are use LINQ or lambda expression queries, but still, sometimes we need to add custom SQL Queries. use using the below code you can achieve that.

Eg: our table name is Logs.

Select Query

dbcontext.Logs.FromSql("select * from Logs");

Select From Stored Procedure, It Can be.ToList(), First() etc depends on you. N number of parameters you can put.

dbcontext.YourStoredProcedureEntityName.FromSqlRaw($"SPName { param1 } ,{ param1  }").ToList();

Insert Update and Delete

context.Database.ExecuteSqlCommand("insert into Logs values(1,'test');")

context.Database.ExecuteSqlCommand("update Logs set Name='test1' where id=1;")

context.Database.ExecuteSqlCommand("delete from Logs where id=1;")