Referring to this thread How to use raw normal sql in ASP.NET MVC without linq?
I know how to use DbContext to map data to Model properties and use them in Razor View. But sometimes I do need to use complex raw SQL query to get data from existing DB tables. From the above link I think I got the idea. SQL to Model, which is exactly what I need. But, my question is, what do I need to write in the DbContext class?
This is from the above reference
static ArticlesDataContext dc = new
ArticlesDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
And in the Controller(...I think),
var arts = dc.ExecuteQuery<Art>(@"Select * from articles");
I guess the ArticlesDataContext class is something like this
public class ArticlesDataContext : DbContext
{
public ArticlesDataContext() : base("name=ConnectionString")
{
}
public DbSet<Article> Articles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
...
// something here to map the DB table columns
}
}
If I need to run the raw SQL query in Controller to get data, what do I do with the DbContext class, which is supposed to get data with EF? I'm confused.
Oh yea, and, what does that "@" do before the SQL statement?
I'm very new to ASP.NET MVC, hope I've made my question clear. Thx.