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 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;
            }
        }
        public void InitialiaseTestRun(string manufactureSerial)
        {
            var currentTestRun = new run();

            var testUnit = DataManager.Instance.GetTestUnit(manufactureSerial);
            if (testUnit != null)
            {
                IsRetest = true;
                // Check if this pcb unit is being tracked in the current DbContext
                pcb_unit localReference = null;
                foreach (var run in _currentSession.runs)
                {
                    if (testUnit.pcb_unit_id == run.pcb_unit.pcb_unit_id)
                        localReference = run.pcb_unit;
                }

                if (localReference != null)
                    currentTestRun.pcb_unit = localReference;
                else
                    currentTestRun.pcb_unit = testUnit;
            }
            else
            {
                IsRetest = false;
                currentTestRun.pcb_unit = new pcb_unit();
                currentTestRun.pcb_unit.pcb_unit_serial_sticker_manufacture = manufactureSerial;
                currentTestRun.pcb_unit.pcb_unit_serial_number = "TEST";
            }

            CreateNewTestRunResponses(currentTestRun);

            _currentSession = DataManager.Instance.AddRun(_currentSession, currentTestRun, !IsRetest);
        }
        public void StartTestSession(string technicianName)
        {
            var technicain = DataManager.Instance.GetTechnician(technicianName);
            _currentSession = new session();
            _currentSession.technician = technicain;
            _currentSession.session_time_stamp = DateTime.Now;

            _currentSession = DataManager.Instance.AddSession(_currentSession);
        }