//Method for Updating Employee record

        public void Add(PCMPreliminaryViewModel vm, int userId, int assID)
        {
            using (SDIIS_DatabaseEntities db = new SDIIS_DatabaseEntities())
            {
                try
                {
                    PCM_Preliminary_Details newPre = new PCM_Preliminary_Details();



                    newPre.Intake_Assessment_Id      = assID;
                    newPre.PCM_Preliminary_Status_Id = vm.PCM_Preliminary_Status_Id;
                    newPre.PreInquiryConducted       = vm.PreInquiryConducted;
                    newPre.ReasonPreInquiryConducted = vm.ReasonPreInquiryConducted;
                    newPre.PCM_Preliminary_Date      = vm.PCM_Preliminary_Date;
                    newPre.PCM_Outcome_Reason        = vm.PCM_Outcome_Reason;
                    newPre.PCM_Offence_Id            = vm.PCM_Offence_Id;
                    newPre.PCM_Recommendation_Id     = vm.PCM_Recommendation_Id;
                    newPre.Modified_By   = userId;
                    newPre.Date_Modified = DateTime.Now;
                    newPre.Modified_By   = userId;
                    newPre.Date_Created  = DateTime.Now;

                    db.PCM_Preliminary_Details.Add(newPre);
                    db.SaveChanges();
                }

                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
        }
        public PCMPreliminaryViewModel Update(PCMPreliminaryViewModel pre, int userId, int PcmPreliID, int AssID)
        {
            PCMPreliminaryViewModel vm = new PCMPreliminaryViewModel();

            using (SDIIS_DatabaseEntities db = new SDIIS_DatabaseEntities())
            {
                try
                {
                    PCM_Preliminary_Details fc = db.PCM_Preliminary_Details.Find(PcmPreliID);
                    if (fc != null)
                    {
                        fc.PCM_Recommendation_Id        = pre.PCM_Recommendation_Id;
                        fc.PCM_Preliminary_Status_Id    = pre.Preliminary_Status_Id;
                        fc.Placement_Pre_Recommended_Id = pre.Placement_Type_Id;
                        fc.PCM_Preliminary_Date         = pre.PCM_Preliminary_Date;
                        fc.PCM_Outcome_Reason           = pre.PCM_Outcome_Reason;

                        fc.Date_Modified = pre.Date_Modified;
                        fc.Modified_By   = pre.Modified_By;
                        fc.Date_Created  = pre.Date_Created;
                        db.SaveChanges();

                        PCM_HB_Supervision exist1 = db.PCM_HB_Supervision.OrderByDescending(x => x.HomeBasedSupervision_Id).FirstOrDefault(x => x.PCM_Preliminary_Id == PcmPreliID);

                        //PCM_HB_Supervision exist1 = db.PCM_HB_Supervision.Where(x => x.PCM_Preliminary_Id == PcmPreliID).SingleOrDefault();
                        if (fc.Placement_Pre_Recommended_Id == 187 && exist1 == null)
                        {
                            PCM_HB_Supervision add = new PCM_HB_Supervision();


                            add.PCM_Preliminary_Id   = PcmPreliID;
                            add.Intake_Assessment_Id = AssID;
                            add.Court_Type_Id        = 1;

                            db.PCM_HB_Supervision.Add(add);
                            db.SaveChanges();
                        }

                        PCM_Diversion exist12 = db.PCM_Diversion.OrderByDescending(x => x.Diversion_Id).FirstOrDefault(x => x.PCM_Preliminary_Id == PcmPreliID);

                        if (fc.PCM_Preliminary_Id == 1 && exist12 == null)

                        {
                            PCM_Diversion newhomebase = new PCM_Diversion();

                            newhomebase.PCM_Preliminary_Id   = fc.PCM_Preliminary_Id;
                            newhomebase.Intake_Assessment_Id = AssID;
                            newhomebase.Court_Type_Id        = 1;

                            db.PCM_Diversion.Add(newhomebase);
                            db.SaveChanges();
                        }
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }

                return(vm);
            }
        }