title: Struggling with saving data to database in ASP.NET MVC with Entity Framework
body: <p>I am just starting to learn C# &amp; ASP.NET MVC framework and I am trying to build a shopping cart functionality. This is my (add to cart method) that I think should work but does not. It adds an item to the cart but whenever I add another item the previous one is gone. Do anyone know why my database saving is not working? Am I using the context the right way?</p>
  
  <pre><code>namespace e_commerse.Controllers
  {
      public class ShoppingcartController : Controller
      {
              private ProductDBContext db = new ProductDBContext();
  
              [Authorize]
              public ActionResult addToCart(int ID)
              {
                  Product product = db.Products.Find(ID);
  
                  if (product == null)
                  {
                      return HttpNotFound();
                  }
  
                  var userId =  User.Identity.GetUserId();
  
                  IEnumerable&lt;ShoppingCart&gt; shoppingcarts = 
                      from cart in db.Shoppingcarts
                      where cart.userID == userId
                      select cart;
  
                  if (!shoppingcarts.Any())
                  {
                      var newShoppingcart = new ShoppingCart();
                      newShoppingcart.products = new List&lt;Product&gt;();
                      newShoppingcart.products.Add(product);
                      newShoppingcart.userID = userId;
                      db.Shoppingcarts.Add(newShoppingcart);
                      db.SaveChanges();
                      return View("Index", newShoppingcart);
                  }
  
                  var shoppingcart = shoppingcarts.First();
  
                  if (shoppingcart.products == null)
                  {
                      shoppingcart.products = new List&lt;Product&gt;();
                  }
  
                  shoppingcart.products.Add(product);
                  shoppingcart.userID = userId;
                  db.SaveChanges();
  
                  return View("Index", shoppingcart);
              }
  
              protected override void Dispose(bool disposing)
              {
                  if (disposing)
                  {
                      db.Dispose();
                  }
                  base.Dispose(disposing);
              }
          }
  
          public class ProductDBContext : DbContext
          {
              public DbSet&lt;Product&gt; Products { get; set; }
              public DbSet&lt;ShoppingCart&gt; Shoppingcarts { get; set; }
          }
  
          public class ShoppingCart
          {
              public int ID { get; set; }
              public String userID { get; set; }
              public List&lt;Product&gt; products { get; set; }
          }
  
          public class Product
          {
              public int ID { get; set; }
              public String title { get; set; }
              public String description { get; set; }
              public decimal price { get; set; }
              public String imagepath { get; set; }
              public Type type { get; set; }
          }
      }
  </code></pre>
  
  <p>Every help is deeply appreciated and excuse my noob code!</p>