Is it possible to run native sql with entity framework?
For .NET Framework version 4 and above: use ObjectContext.ExecuteStoreCommand()
if your query returns no results, and use ObjectContext.ExecuteStoreQuery
if your query returns results.
For previous .NET Framework versions, here's a sample illustrating what to do. Replace ExecuteNonQuery() as needed if your query returns results.
static void ExecuteSql(ObjectContext c, string sql)
{
var entityConnection = (System.Data.EntityClient.EntityConnection)c.Connection;
DbConnection conn = entityConnection.StoreConnection;
ConnectionState initialState = conn.State;
try
{
if (initialState != ConnectionState.Open)
conn.Open(); // open connection if not already open
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
finally
{
if (initialState != ConnectionState.Open)
conn.Close(); // only close connection if not initially open
}
}
How to execute raw SQL query using Entity Framework without having to use a model?
Specify string
as the type argument.
var results = db1New.Database.SqlQuery<string>("SELECT LOWER(column_name) AS column_name FROM information_schema.columns WHERE table_name = @p0", tableName).ToArray();
^^^^^^
How to execute raw sql query with in entity framework?
You can execute the following types of queries:
SQL query for entity types which returns particular types of entities.
using (var ctx = new SchoolDBEntities())
{
var studentList = ctx.Students.SqlQuery("Select * from Student").ToList<Student>();
}SQL query for non-entity types which returns a primitive data type.
using (var ctx = new SchoolDBEntities())
{
var studentName = ctx.Students.SqlQuery("Select studentid, studentname
from Student where studentname='New Student1'").ToList();
}
//Error
using (var ctx = new SchoolDBEntities())
{
//this will throw an exception
var studentName = ctx.Students.SqlQuery("Select studentid as id, studentname as name
from Student where studentname='New Student1'").ToList();
}
//SQL query for non-entity types:
using (var ctx = new SchoolDBEntities())
{
//Get student name of string type
string studentName = ctx.Database.SqlQuery<string>("Select studentname
from Student where studentid=1").FirstOrDefault<string>();
}Raw SQL commands to the database.
using (var ctx = new SchoolDBEntities())
{
//Update command
int noOfRowUpdated = ctx.Database.ExecuteSqlCommand("Update student
set studentname ='changed student by command' where studentid=1");
//Insert command
int noOfRowInserted = ctx.Database.ExecuteSqlCommand("insert into student(studentname)
values('New Student')");
//Delete command
int noOfRowDeleted = ctx.Database.ExecuteSqlCommand("delete from student
where studentid=1");
}
You can also refer this
Related Topics
Is the Like Operator Case-Sensitive with SQL Server
How to Declare Global Variable in SQL Server..
How to Sort a Varchar Column in SQL Server That Contains Numbers
Solution For: Store Update, Insert, or Delete Statement Affected an Unexpected Number of Rows (0)
Why Are Relational Set-Based Queries Better Than Cursors
SQL Error: Ora-01861: Literal Does Not Match Format String 01861
How to Create Table Using Select Query in SQL Server
Find the Smallest Unused Number in SQL Server
SQL Query - Concatenating Results into One String
How to Calculate a Running Total in SQL Without Using a Cursor
How to Prevent a Database Trigger from Recursing
Mysql: What Is a Reverse Version of Like
How to Import a Large Ms SQL .SQL File
SQL Server Date Format Function
SQL Server Like Containing Bracket Characters
Split Values Over Multiple Rows
How to Query for All Dates Greater Than a Certain Date in SQL Server