I and V E R Y new to learning ASP.NET MVC core, and I have hit a problem which is vexing me (and I hope someone can point out where I'm going wrong).
I have following a course on Puralsight (https://app.pluralsight.com/library/courses/aspdotnetcore-web-application-building/table-of-contents), and have got to Module 6, section 7 (Creating a Simple View Component). It shows you how to add a nav bar a shopping cart (link item counter) onto every page. When I cut'n'paste the code snippet into the corresponding pages, then launch IIS express to view the website, the website fails with this message in the browser :
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 highlights this code (specifically, within the curly braces) within the file ShoppingCart.cs
public List<ShoppingCartItem> GetShoppingCartItems()
{
return ShoppingCartItems ??
(ShoppingCartItems =
_appDbContext.ShoppingCartItems.Where(c => c.ShoppingCartId == ShoppingCartId)
.Include(s => s.Pie)
.ToList());
}
with the error message
Exception User-Unhandled
System.Data.SqlClient.SqlException: Invalid Object name 'ShoppingCartItems'.'
I have managed to locate the line of code that , when commented out, allows the site to load (minus the shopping cart details). It is in the _Layout.chtml file, within the SHARED folder :-
@await Component.InvokeAsync('ShoppingCartSummary')
Commenting out this line allows the site to load, but that's only because it not calling the Shopping cart (so, akin to saying you Car is fixed, as long as you don't use it).
I have a ShoppingCartSummary.cs file with the line
var items = _shoppingCart.GetShoppingCartItems();
which I have replaced with
var items = new List<ShoppingCartItem>() { new ShoppingCartItem(), new ShoppingCartItem() };
so the cart has dummy entries, but I still get the same error (System.Data.SqlClient.SqlException: Invalid Object name 'ShoppingCartItems'.') when launching IISExpress.
Is anybody able to point out what Is missed/done wrong ?
Thanks in advance
ShoppingCartItemsdoesn't exist in your dbAdd-Migration AddShoppingCartItemthenUpdate-Databaseand I can see a new .cs file in the Migrations folder called 20170612093119_AddShoppingCartItem.cs (along with 20170612030702_initial.cs). With the@await Component.InvokeAsync('ShoppingCartSummary')code comment out, the website launches fine, including reference to tables reference in the AppDbContext file (which includes the linepublic DbSet<ShoppingCartItem> ShoppingCartItems { get; set; }, so I am not sure why (when the data for the others (PIE and CATEGORIES) is access/listed.dbo.Piesanddbo.Categories, but no ShoppingCartItems. So, does this mean my AppDbContext is not functioning correctly ?Update-Databaseinstead onDoneI seeSystem.Data.SqlClient.SqlException: There is already an object named 'Categories' in the database. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection,I'm guessing that the update fails because of theThere is already an object named 'Categories' in the databasemessage....if I'm correct, how do I rectify this ?