Search code examples
c#asp.netdatabaserazordatabase-connection

ASP.Net Razor Web Application Connect to LocalDB issues; Database does not exist in this context


I'm programming a Razor website from the Ground up using VS 2013. I started with a blank Web Application Project and slowly added files and functionality to suit what I need. Basically its just jquery ui, css, and a few basic test webpages.

Keep in mind, I have added a reference to: WebMatrix.Data.dll

Now when I try to connect to my database, I use the following code:

@{
Layout = @"~\_SiteLayout.cshtml";
Page.Title = "Main Tabs";
var DB = Database.Open("NASDB");
var QString = "SELECT * from Tabpage";
}

But this code doesn't work. It says "Database does not exist in this context."

But when I use this code everything works:

@{
Layout = @"~\_SiteLayout.cshtml";
Page.Title = "Main Tabs";
var DB = WebMatrix.Data.Database.Open("NASDB");
var QString = "SELECT * from Tabpage";
}

This is functional, but not easily readable. Is there any way to fix this? So just Database will work. I also have to have the "Copy Local" property of the "WebMatrix.Data" reference set to true. Is there any way to avoid this as well?


Solution

  • At the top of the page, add:

    @using Webmatrix.Data
    

    or you can add a reference in the web.config file that exists in your views folder.