Я пытаюсь научиться использовать функцию корзины покупок в MVC и пытаюсь адаптировать код MusicStore из учебников на сайте asp.net для своих собственных целей.
Так что я использовал это и попытался изменить его в соответствии с моими потребностями. Я немного изменил его для работы с макетом моей базы данных и дошел до того, что попытался добавить статью в корзину, и вот где я застрял некоторое время. Попытка добавить товар в корзину приводит к сбою.
Invalid column name 'Articles_ArtikelNr'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'Articles_ArtikelNr'.
Source Error:
Line 28: {
Line 29: // Get the matching cart and album instances
Line 30: var cartItem = storeDB.Cart.SingleOrDefault(
Line 31: c => c.CartId == ShoppingCartId
Line 32: && c.ArtId == id);
Модель выглядит так:
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public string ArtId { get; set; }
public int Count { get; set; }
public decimal Price { get; set; }
public DateTime DateCreated { get; set; }
public virtual Art Articles { get; set; }
}
И контроллер:
public ActionResult AddToCart(string id)
{
// Retrieve the album from the database
//var addedProduct = storeDB.Art
// .Single(Art => Art.ArtikelNr == id);
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCart(id);
// Go back to the main store page for more shopping
return RedirectToAction("Index", "Home", cart);
}
Вы можете видеть некоторые вещи, которые я изменил, я не вижу никакой причины перемещать больше, чем просто ID (номер статьи) в настоящее время, делая его простым ...
Вот также сама функция, где происходит сбой:
public void AddToCart(string id)
{
// Get the matching cart and album instances
var cartItem = storeDB.Cart.SingleOrDefault(
c => c.CartId == ShoppingCartId
&& c.ArtId == id);
if (cartItem == null)
{
// Create a new cart item if no cart item exists
cartItem = new Cart
{
ArtId = id,
CartId = ShoppingCartId,
Count = 1,
Price = 1,
DateCreated = DateTime.Now
};
storeDB.Cart.Add(cartItem);
}
else
{
// If the item does exist in the cart,
// then add one to the quantity
cartItem.Count++;
}
// Save changes
storeDB.SaveChanges();
}
Как обычно, я уверен, что есть кое-что действительно простое, что я упустил из виду. Я только учусь, но я подозреваю, что эти article_ArtikelNr относится к
public virtual Art Articles { get; set; }
часть в модели и article_ArtikelNr, но я не уверен, что здесь не так. что-то связанное с тем, как набирается запрос cartItem?