2017-06-12 299 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文件的初始問題。

相關問題