异常User-Unhandled System.Data.SqlClient.SqlException:无效的对象名称

时间:2017-06-12 09:56:07

标签: asp.net-mvc

我和 非常 是学习ASP.NET MVC核心的新手,我遇到了一个令我烦恼的问题(我希望有人可以指出我在哪里'我错了)。

我已经学习了关于Puralsight(https://app.pluralsight.com/library/courses/aspdotnetcore-web-application-building/table-of-contents)的课程,并参加了第6部分第7节(创建一个简单的视图组件)。 它向您展示了如何将购物车(链接项计数器)导航栏添加到每个页面上。 当我将代码片段切换到相应的页面时,然后启动IIS express以查看该网站,该网站在浏览器中失败并显示此消息:

    HTTP Error 502.3 - Bad Gateway
The specified CGI application encountered an error and the server terminated the process.
Most likely causes:
The CGI application did not return a valid set of HTTP errors.
A server acting as a proxy or gateway was unable to process the request due to an error in a parent gateway.
Things you can try:
Use DebugDiag to troubleshoot the CGI application.
Determine if a proxy or gateway is responsible for this error.
Detailed Error Information:
Module
    AspNetCoreModule
Notification
    ExecuteRequestHandler
Handler
    aspNetCore
Error Code
    0x80072ee2
Requested URL
    http://localhost:64923
Physical Path
    c:\users\MYDIRECTORY\visual studio 2017\Projects\BethanysPieShop\BethanysPieShop
Logon Method
    Anonymous
Logon User
    Anonymous
Request Tracing Directory

Visual Studio Community 2017在ShoppingCart.cs文件中突出显示此代码(特别是在大括号内)

        public List<ShoppingCartItem> GetShoppingCartItems()
    {
        return ShoppingCartItems ??
               (ShoppingCartItems =
                   _appDbContext.ShoppingCartItems.Where(c => c.ShoppingCartId == ShoppingCartId)
                       .Include(s => s.Pie)
                       .ToList());
    }

带有错误消息

Exception User-Unhandled
System.Data.SqlClient.SqlException: Invalid Object name 'ShoppingCartItems'.'

我已设法找到代码行,当注释掉时,允许网站加载(减去购物车详细信息)。它位于_Layout.chtml文件中,位于SHARED文件夹中: -

@await Component.InvokeAsync('ShoppingCartSummary')

注释掉这一行可以加载网站,但这只是因为它没有调用购物车(所以,就像你说汽车是固定的,只要你不使用它)。

我有一个带有

行的ShoppingCartSummary.cs文件
    var items = _shoppingCart.GetShoppingCartItems();

我用

取代了
var items = new List<ShoppingCartItem>() { new ShoppingCartItem(), new ShoppingCartItem() };

所以购物车有虚拟条目,但在启动IISExpress时我仍然得到相同的错误(System.Data.SqlClient.SqlException:无效的对象名称'ShoppingCartItems'。'。)

是否有人能够指出错过/做错了什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

好的,我现在已经对它进行了分类,所以将其发布给其他有类似问题的人。

There is already an object named 'Categories' in the database message

是一只红鲱鱼。 解决了什么问题,删除整个数据库(通过SQL Server对象资源管理器,右键单击数据库并选择删除)...授予,而不是某些人的理想解决方案,然后,在解决方案资源管理器中,删除迁移下的所有内容文件夹(再次,右键单击,删除)。 关闭VS.打开VS. 打开Sql Server对象资源管理器。 打开包管理器,然后运行

Add-Migration initial

然后

Update-Database

然后,您可以在SQL Server对象管理器中查看数据库和故事,并创建缺失的表。

我假设_MigrationHistory文件存在初始问题。