Search code examples
c#databaserazorvisual-studio-2013vs-web-site-project

Visual Studio 2013 WebSite Project Database.Open


Hello i have created an empty Website C#. I created a database and i am trying to read the contents from a .cshtml

    @{
    var db = WebMatrix.Data.Database.Open("Test");
    var selectQueryString = "SELECT * FROM Product ORDER BY Name";
}
<html>
<body>
    <h1>Small Bakery Products</h1>
    <table>
        <tr>
            <th>Id</th>
            <th>Product</th>
            <th>Description</th>
            <th>Price</th>
        </tr>
        @foreach (var row in db.Query(selectQueryString))
        {
            <tr>
                <td>@row.Id</td>
                <td>@row.Name</td>
                <td>@row.Description</td>
                <td align="right">@row.Price</td>
            </tr>
        }
    </table>
</body>
</html>

at first i got an error

Error 1 The name 'Database' does not exist in the current context

which was fixed after i did this https://www.nuget.org/packages/WebMatrix.WebData/

now i get a message error

Error 1 The pre-application start initialization method Start on type WebMatrix.WebData.PreApplicationStartCode threw an exception with the following error message: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method 'System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed..


Solution

  • Sounds like this: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()'

    "For me this error was because I did NOT have Microsoft.AspNet.WebHelpers installed after updating from MVC 4 to MVC 5. It was fixed by installing the NuGet package"

    Install-Package -Id  Microsoft.AspNet.WebHelpers