public void TestSaevSession()
        {
            using (Entities entities = new Entities())
            {
                try
                {
                    entities.Database.ExecuteSqlCommand("DELETE FROM sessions");
                }
                catch (Exception ex)
                { }

                try
                {
                    entities.Database.ExecuteSqlCommand("DELETE FROM technicians");
                }
                catch (Exception ex)
                { }
                entities.SaveChanges();
            }

            DataManager.Instance.AddTechnician("dan");

            using (Entities entities = new Entities())
            {
                Assert.AreEqual(1, entities.technicians.Count());
            }

            technician technicianDan;
            using (Entities entities = new Entities())
            {
                technicianDan = entities.technicians.FirstOrDefault(t => t.technician_name == "dan");
                Assert.IsNotNull(technicianDan);
            }

            session newSession = new session { technician = technicianDan, session_time_stamp = DateTime.Now };

            using (Entities entities = new Entities())
            {
                entities.sessions.Attach(newSession);
                newSession = entities.sessions.Add(newSession);
                Assert.IsTrue(entities.SaveChanges() > 0);
                Assert.IsNotNull(newSession);
            }
        }
        public session SetCurrentRunComplete(session currentSession)
        {
            try
            {
                using (Entities entities = new Entities())
                {
                    entities.sessions.Attach(currentSession);
                    entities.runs.Attach(currentSession.runs.Last());
                    currentSession.runs.Last().run_complete_timestamp = DateTime.Now; // Update on every save
                    currentSession.runs.Last().run_complete = 1;

                    entities.SaveChanges();
                    return currentSession;
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                throw;
            }
        }
 public bool SaveSession(session currentSession)
 {
     try
     {
         using (Entities entities = new Entities())
         {
             entities.sessions.Attach(currentSession);
             return entities.SaveChanges() > 0;
         }
     }
     catch (Exception ex)
     {
         _log.Error(ex);
         throw;
     }
 }
        public session SaveResponses(session currentSession)
        {
            try
            {
                using (Entities entities = new Entities())
                {
                    entities.sessions.Attach(currentSession);
                    entities.runs.Attach(currentSession.runs.Last());

                    currentSession.runs.Last().run_complete_timestamp = DateTime.Now; // Update on every save
                    foreach (var responseItem in currentSession.runs.Last().responses)
                    {
                        entities.responses.Attach(responseItem);
                        entities.Entry(responseItem).State = EntityState.Modified;
                    }

                    entities.SaveChanges();
                    return currentSession;
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                throw;
            }
        }
 public bool AddTechnician(string technicianName)
 {
     try
     {
         using (Entities entities = new Entities())
         {
             entities.technicians.Add(new technician { technician_name = technicianName, technician_create_timestamp = DateTime.Now });
             return entities.SaveChanges() > 0;
         }
     }
     catch (Exception ex)
     {
         _log.Error(ex);
         throw;
     }
     return false;
 }
 public session AddSession(session currentSession)
 {
     try
     {
         using (Entities entities = new Entities())
         {
             // Attach first so that the child objects (technician in this case) is bound to the new data context
             entities.sessions.Attach(currentSession);
             entities.sessions.Add(currentSession);
             entities.SaveChanges();
             return currentSession;
         }
     }
     catch (Exception ex)
     {
         _log.Error(ex);
         throw;
     }
 }
        public session AddRun(session currentSession, run currentRun, bool isNewPCB)
        {
            try
            {
                using (Entities entities = new Entities())
                {
                    entities.sessions.Attach(currentSession);

                    currentRun.run_complete_timestamp = currentRun.run_start_timestamp = DateTime.Now;
                    currentRun.session = currentSession;

                    currentSession.runs.Add(currentRun);
                    entities.runs.Add(currentRun);

                    foreach (var responseItem in currentRun.responses)
                    {
                        entities.responses.Add(responseItem);
                    }

                    if (isNewPCB)
                    {
                        entities.pcb_unit.Add(currentRun.pcb_unit);
                    }
                    else
                    {
                        //entities.Entry(pcbUnit).State = EntityState.Unchanged;
                        entities.pcb_unit.Attach(currentRun.pcb_unit);
                    }

                    entities.SaveChanges();
                    return entities.sessions.First(s => s.session_id == currentSession.session_id);

                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                throw;
            }
        }