title: Foreach tag to loop through c# object and insert to database using Entity Framework
body: <p>Example of Input Json Format how it looks like:</p>
  
  <pre><code>    {
            Reservations:
            [
                {
                     FacilityReservationID:"....",
                     FacilityID:"....",
                     Description:"...."
                },
                     ........
            ]
      }
  </code></pre>
  
  <p>Return Result format:</p>
  
  <pre><code>    {
              Result:"OK"/Error",
              Message:"...."
      }
  </code></pre>
  
  <p>I created a asp.net webform to accept a webservice call from a console app program in Json Restful.</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using Newtonsoft.Json;
      using System.Data.Entity;
  
      namespace ReservationListWebService
      {
          public partial class UpdateFacility : System.Web.UI.Page
          {
  </code></pre>
  
  <p>I created a context to call the EF Database.</p>
  
  <pre><code>            public class KioskContext : DbContext
              {
                 //using the FacilityReservationKioskEntities Connection string
                 public KioskContext()
                   : base("name=FacilityReservationKioskEntities")
                 {
                 }
                 public DbSet&lt;Department&gt; Departments { get; set; }
                 public DbSet&lt;Facility&gt; Facilitys { get; set; }
                 public DbSet&lt;FacilityReservation&gt; Reservations { get; set; }
            }
  
  
            public class ReservationList
            {
                 public string facilityReservationID { get; set; }
                 public string facilityID { get; set; }
                 public DateTime startDateTime { get; set; }
                 public DateTime endDateTime { get; set; }
                 public string useShortDescription { get; set; }
                 public string useDescription { get; set; }
            }
  </code></pre>
  
  <p>The console app would call my webform and hence pass me the input of a string. After deserialized into a c# object and i would eventually insert it into the database using Entity framework format. I am not sure what i should input inside the foreach() tag to loop through the c# objects and insert it into the database. I am stuck with the foreach loop. </p>
  
  <p>And also how am i going to return the output result to the console app that pass me the Json data? Please help and guide! thank you!</p>
  
  <pre><code>          protected void Page_Load(object sender, EventArgs e)
            {
                 string departmentID = Request.QueryString["DepartmentID"];
                 string json = Request.Form["Json"];
  
                 ReservationList list = JsonConvert.DeserializeObject&lt;ReservationList&gt;(json);
  
                 using (var db = new KioskContext())
                 {
                       db.Database.ExecuteSqlCommand(
                             "DELETE FacilityReservation FROM Department INNER JOIN Facility ON Department.DepartmentID = Facility.DepartmentID" +
                      "INNER JOIN FacilityReservation ON Facility.FacilityID = FacilityReservation.FacilityID WHERE Department.DepartmentID = '" + departmentID + "'");
  
                  foreach(....)
                  {
                       FacilityReservation res = new FacilityReservation();
  
                       //set all fields here
                       res.FacilityReservationID = list.facilityReservationID;
  
                       db.Reservations.Add(res);
                  }
                  db.SaveChanges();
                }
            }
        }
    }
  </code></pre>