Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run native sql with entity framework?

I am trying to search an XML field within a table, This is not supported with EF.

Without using pure Ado.net is possible to have native SQL support with EF?

like image 838
user80855 Avatar asked May 27 '09 12:05

user80855


People also ask

Can we use SQL query in Entity Framework?

Entity Framework allows you to execute raw SQL queries for the underlying relational database.

How use raw SQL query in Entity Framework Core?

Entity Framework Core allows you to drop down to raw SQL queries when working with a relational database. Raw SQL queries are useful if the query you want can't be expressed using LINQ. Raw SQL queries are also used if using a LINQ query is resulting in an inefficient SQL query.

How do I get SQL generated by Entity Framework?

To view the SQL that will be generated, simply call ToTraceString() . You can add it into your watch window and set a breakpoint to see what the query would be at any given point for any LINQ query. You can attach a tracer to your SQL server of choice, which will show you the final query in all its gory detail.


2 Answers

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     } } 
like image 98
Justin Grant Avatar answered Sep 18 '22 15:09

Justin Grant


Using Entity Framework 5.0 you can use ExecuteSqlCommand to execute multi-line/multi-command pure SQL statements. This way you won't need to provide any backing object to store the returned value since the method returns an int (the result returned by the database after executing the command).

Sample:

context.Database.ExecuteSqlCommand(@ "-- Script Date: 10/1/2012 3:34 PM  - Generated by ExportSqlCe version 3.5.2.18 SET IDENTITY_INSERT [Students] ON;  INSERT INTO [Students] ([StudentId],[FirstName],[LastName],[BirthDate],[Address],[Neighborhood],[City],[State],[Phone],[MobilePhone],[Email],[Enrollment],[Gender],[Status]) VALUES (12,N'First Name',N'SecondName',{ts '1988-03-02 00:00:00.000'},N'RUA 19 A, 60',N'MORADA DO VALE',N'BARRA DO PIRAÍ',N'Rio de Janeiro',N'3346-7125',NULL,NULL,{ts '2011-06-04 21:25:26.000'},2,1);  INSERT INTO [Students] ([StudentId],[FirstName],[LastName],[BirthDate],[Address],[Neighborhood],[City],[State],[Phone],[MobilePhone],[Email],[Enrollment],[Gender],[Status]) VALUES (13,N'FirstName',N'LastName',{ts '1976-04-12 00:00:00.000'},N'RUA 201, 2231',N'RECANTO FELIZ',N'BARRA DO PIRAÍ',N'Rio de Janeiro',N'3341-6892',NULL,NULL,{ts '2011-06-04 21:38:38.000'},2,1); "); 

For more on this, take a look here: Entity Framework Code First: Executing SQL files on database creation

like image 44
Leniel Maccaferri Avatar answered Sep 17 '22 15:09

Leniel Maccaferri