title: How to execute raw SQL query using Entity Framework without having to use a model?
body: <p>I am trying to learn C# ASP.NET MVC 5. And I am trying to use Entity Framework for everything I do. </p>
  
  <p>However, I need to run a raw SQL query and return the results into an array.</p>
  
  <p>Here is what I have done so far.</p>
  
  <p>I created my context class which allows me to connect to a server and it also allows me to change the database at run time.</p>
  
  <p>Here is my context class</p>
  
  <pre><code>using ScripterEngine.Models;
  using System;
  using System.Collections.Generic;
  using System.Data.Common;
  using System.Data.Entity;
  using System.Data.Entity.Core.EntityClient;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Web;
  
  namespace ScripterEngine.DataAccessLayer
  {
      public class BaseContext : DbContext
      {
          protected string connectionName;
          public DbSet&lt;Campaign&gt; Campaign { get; set; }
  
          /**
           * Created the connection to the server using the giving connection string name
           * 
           * @param connName
           */
          public BaseContext(string connName = "BaseConnection")
              : base(connName)
          {
              connectionName = connName;
          }
  
          /**
           * Changes the default database
           * 
           * @param databaseName
           */
          public BaseContext setDatabase(string databaseName)
          {
              var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
  
              SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
  
              //change the database before creating the new connection
              builder.InitialCatalog = databaseName;
  
              string sqlConnectionString = builder.ConnectionString;
  
              return new BaseContext(sqlConnectionString);
          }
      }
  }
  </code></pre>
  
  <p>And how to make the connection here is what I do</p>
  
  <pre><code>BaseContext db1 = new BaseContext("server1");
  var db1New = db1.setDatabase("someTableName");
  string tableName = "SomeTableName";
  
  var results = db1New.Database.SqlQuery("SELECT LOWER(column_name) AS column_name FROM information_schema.columns WHERE table_name = @tableName", tableName).ToArray();
  </code></pre>
  
  <p>This throws an error</p>
  
  <blockquote>
    <p>The type arguments for method 'System.Data.Entity.Database.SqlQuery(string, params object[])' cannot be inferred from the usage. Try specifying the type arguments explicitly.  C:.NET Projects\ScripterEngine\ScripterEngine\Controllers\CampaignController.cs    42  27  ScripterEngine</p>
  </blockquote>
  
  <p>How can I execute this raw query?</p>