2017-06-12 301 views
0

我和非常新学习ASP.NET MVC的核心,我已经打到这是伤脑筋我的问题(我希望有人能指出我要出错的地方)。异常用户未处理System.Data.SqlClient.SqlException:无效的对象名称

我有一门关于Puralsight的课程(https://app.pluralsight.com/library/courses/aspdotnetcore-web-application-building/table-of-contents),并且有模块6的第7节(创建简单视图组件)。 它向您展示了如何将购物车(链接项目计数器)的导航栏添加到每个页面上。 当我cut'n'paste代码段到相应的页面,然后启动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 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文件,共享文件夹内: -

@await Component.InvokeAsync('ShoppingCartSummary') 

注释掉这一行允许网站的加载,但是这仅仅是因为它没有要求的购物车(所以,类似于说你车是固定的,只要你不使用它)。

我有一个ShoppingCartSummary.cs文件与我同

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

所以车中有虚条目取代了线

var items = _shoppingCart.GetShoppingCartItems(); 

,但我仍然得到同样的错误(系统。 Data.SqlClient.SqlException:无效的对象名称'ShoppingCartItems'。')启动IISExpress时。

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

在此先感谢

+0

你的错误是说对象'ShoppingCartItems'不存在于你的分区 – jamiedanq

+0

我运行了'Add-Migration AddShoppingCartItem'然后'Update-Database',我可以在Migrations文件夹中看到一个新的.cs文件20170612093119_AddShoppingCartItem.cs(与20170612030702_initial.cs一起)。通过'@await Component.InvokeAsync('ShoppingCartSummary')'代码注释掉,网站启动正常,包括引用AppDbContext文件中的表引用(其中包括'public DbSet ShoppingCartItems {get; set;} ',所以我不知道为什么(当其他人的数据(PIE和CATEGORIES)被访问/列出时 –

+0

使用SQL Server对象资源管理器,我可以深入查看LocalDB \ Databases \ ProjectName \ Tables并查看'dbo.Pies '和'dbo.Categories',但是没有ShoppingCartItems,那么这是否意味着我的AppDbContext运行不正常? –

回答

0

OK,我有现在这个排序,所以这张贴了其他任何人有类似的问题。

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文件的初始问题。

相关问题