title: Store list of Products in database
body: <p>I am challenging myself to make a small web-shop, but I have faced a problem while storing an Order in my DB.</p>
  
  <p>This is my Order class</p>
  
  <pre><code>    [Key]
      public int Id { get; set; }
      public IEnumerable&lt;BasketProduct&gt; BasketProducts { get; set; }
      public string UserName { get; set; }
  </code></pre>
  
  <p>which stores BasketProducts(without int Id it did not want to work, but I don't know why it needs it)</p>
  
  <pre><code>    public int Id { get; set; }
      public Product Product { get; set; }
      public int Quantity { get; set; }
  </code></pre>
  
  <p>In the BasketController, if the user clicks for a checkout I would like to create an order and put it into the DB</p>
  
  <pre><code>public ActionResult CreateOrder()
          {
              //GetCurrentBasket returns a basket of current session
              var baskItems = GetCurrentBasket().Items;
  
              Order order = new Order { BasketProducts = baskItems, UserName = User.Identity.Name };
              EFDbContext ent = new EFDbContext();
              ent.Orders.Add(order);
              ent.SaveChanges();
  
              return RedirectToAction("Index", "Checkout", new { order = order });
          }
  </code></pre>
  
  <p>And in checkout Index method it should just take Order object and show all the products in it</p>
  
  <pre><code>       @model Domain.Elements.Order
         //some code omitted
         @foreach (var item in Model.BasketProducts)
         {
            &lt;p&gt;@item.Product.Name&lt;/p&gt;
            &lt;p&gt;@item.Quantity&lt;/p&gt;
         }
  </code></pre>
  
  <p>The problem is that in my DB(in Order table) it creates only Id and UserName, so I cannot retrieve any products current person ordered.</p>
  
  <p>What mistake did I do, or maybe I am missing something to make it work? Thanks in advance.</p>
  
  <p>Edit: This is the error I get right now.
  Object reference not set to an instance of an object.</p>