title: Insert multiple items in List before inserting into database
body: <p>So I have the relation in the database as follows: </p>
  
  <p><img src="https://i.stack.imgur.com/LfeTM.jpg" alt="enter image description here"></p>
  
  <p>The idea is to add multiple product categories into the list before inserting it into the database. </p>
  
  <p>I'm using ADO.NET framework, and the mapped class products contains list of product categories. So when the user fills out the form that is needed for product category I say:</p>
  
  <pre><code>Products p = new Products(... initializing);
  ProductCategories p2  = new ProductCategories(...initializing);
  p.ProductCategories.Add(p2);
  </code></pre>
  
  <p>What I don't understand is how to add more of these ProductCategories items into this list before I insert them into the database. Whenever I add new one, the old one seems to be deleted from the list, and I don't understand why? The order should be like this:</p>
  
  <p>1st product category item
  2nd 
  3rd...</p>
  
  <p>(If the user stops adding them....) -> click save button -> insert all product categories from the list into product categories table...</p>
  
  <p>Instead of that I just get overwritten old product category with new values that I just inserted. Can someone help me out with this?? </p>
  
  <p>EDIT: here is the entire code responsible for adding multiple items to the list</p>
  
  <pre><code>       if (FileUpload1.PostedFile != null &amp;&amp; FileUpload1.PostedFile.ContentLength &gt; 0)
                      {
                          p = new Product();
  // p is declared as property outside of all methods...
                          p.ProductName =  txtProductTitle.Text;
                          p.Dimensions = ".";
                          p.PricePerUnit = float.Parse(txtProductPrice.Text);
                          System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
                          MemoryStream mem = new MemoryStream();
                          img.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
                          p.ProductPicture = mem.ToArray();
                          p.ProductPicThumb = mem.ToArray();
                          p.MeasurementUnitID = int.Parse(dropMeasure.SelectedValue);
                          p.PricePerFeet = 1.2F;
                        int pid =  ServiceClass.InsertProduct(p);
          ProductCategories p2  = new ProductCategories(...initializing);
          p.ProductCategories.Add(p2);
                          Response.Write("&lt;script&gt;alert('Product successfully added!');&lt;/script&gt;");
                      }
                      else
                      {
                          Response.Write("&lt;script&gt;alert('Something went wrong!');&lt;/script&gt;");
                      }
  </code></pre>