EF 4.1 Code The first few result sets

I need to execute a Raw SQL query that returns multiple result sets. Is this possible in EF CF?

Thanks!

+6
source share
1 answer

Description

Yes, you can !;) You can execute a raw SQL query using DbCommand in the Entity Framework Code First too.

You can execute queries with multiple result sets and move on to the next result set using the NextResult() method of the SqlDataReader class.

Example

 namespace MyNamespace { public class MyDbContext : DbContext { } public class Test { public void Test() { MyDbContext context = new MyDbContext(); DbCommand db = context.Database.Connection.CreateCommand(); db.CommandText = "SELECT propertie1 FROM Table1; SELECT propertie1 from Table2"; try { DbDataReader reader = db.ExecuteReader(); while (reader.Read()) { // read your data from result set 1 string value = Convert.ToString(reader["propertie1"]); } reader.NextResult(); while (reader.Read()) { // read your data from result set 2 string value = Convert.ToString(reader["propertie1"]); } } catch { // Exception handling } finally { if (db.Connection.State != ConnectionState.Closed) db.Connection.Close(); } } } } 

Additional Information

+3
source

Source: https://habr.com/ru/post/902675/


All Articles