Visual Studio 2013 年网站项目 Database.Open

Visual Studio 2013 WebSite Project Database.Open

您好,我创建了一个空网站 C#。我创建了一个数据库,我正在尝试从 .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>

起初我得到一个错误

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

在我这样做后修复了 https://www.nuget.org/packages/WebMatrix.WebData/

现在我收到一条消息错误

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..

听起来像这样: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