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

StackOverflow https://stackoverflow.com/questions/23182690

Question

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?

Était-ce utile?

La solution

At The top of the page:

@using Webmatrix.Data

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top