public CACCCheckInDb.Person UpdatePeople(CACCCheckInDb.Person updatedPerson)
        {
            logger.Debug("Opening DataContext to CACCCheckIn DB.");
            CACCCheckInDb.CACCCheckInDbDataContext db = new CACCCheckInDb.CACCCheckInDbDataContext();
            db.Log = logwriter;

            logger.DebugFormat("Querying CACCCheckIn DB for Person with PersonId=[{0}].",
                               updatedPerson.Id.ToString("B"));
            CACCCheckInDb.Person currentPerson = (from p in db.Persons
                                                  where p.Id.Equals(updatedPerson.Id)
                                                  select p).Single <CACCCheckInDb.Person>();

            logger.DebugFormat("Updating Person: [{0} {1}]",
                               updatedPerson.FirstName, updatedPerson.LastName);
            currentPerson.FirstName         = updatedPerson.FirstName;
            currentPerson.LastName          = updatedPerson.LastName;
            currentPerson.PhoneNumber       = updatedPerson.PhoneNumber;
            currentPerson.FamilyId          = updatedPerson.FamilyId;
            currentPerson.FamilyRole        = updatedPerson.FamilyRole;
            currentPerson.SpecialConditions = updatedPerson.SpecialConditions;

            db.SubmitChanges();

            return(db.Persons.Where(p =>
                                    p.Id.Equals(updatedPerson.Id)).Single <CACCCheckInDb.Person>());
        }
        public void DeletePeople(CACCCheckInDb.Person person)
        {
            logger.Debug("Opening DataContext to CACCCheckIn DB.");
            CACCCheckInDb.CACCCheckInDbDataContext db = new CACCCheckInDb.CACCCheckInDbDataContext();
            db.Log = logwriter;

            try
            {
                logger.DebugFormat("Attaching Person: [{0} {1}] to DataContext",
                                   person.FirstName, person.LastName);
                db.Persons.Attach(person);
                logger.DebugFormat("Deleting Person: [{0} {1}]",
                                   person.FirstName, person.LastName);
                db.Persons.DeleteOnSubmit(person);

                db.SubmitChanges();
            }
            catch (ChangeConflictException ex)
            {
                logger.Error("ChangeConflictException:", ex);
                foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                {
                    occ.Resolve(RefreshMode.OverwriteCurrentValues);
                }
            }
        }
        /// <summary>
        /// Whenever a Person in the collection has had its EndEdit
        /// </summary>
        /// <param name="sender"></param>
        private void PeopleItemEndEdit(IEditableObject sender)
        {
            try
            {
                // Get the Person that has been edited. This could be a new
                // class or an edit to existing Person.
                CACCCheckInDb.Person updatedPerson = sender as
                                                     CACCCheckInDb.Person;

                // If the RowTimestamp is null, this must be brand new Person
                // that hasn't been added to the database yet. If not, this
                // must be an update to existing Person
                if (updatedPerson.RowTimestamp == null)
                {
                    logger.DebugFormat("Adding person [{0} {1}]",
                                       updatedPerson.FirstName, updatedPerson.LastName);

                    updatedPerson = CACCCheckInDb.People.InsertPerson(updatedPerson);

                    View.ViewDispatcher.BeginInvoke(DispatcherPriority.DataBind,
                                                    new DispatcherOperationCallback(
                                                        delegate(object arg)
                    {
                        View.PersonInsertCompleted(updatedPerson);
                        return(null);
                    }), null);

                    CACCCheckInDb.ClassMember newClassMember = new CACCCheckInDb.ClassMember();
                    newClassMember.ClassId   = View.CurrentClass.Id;
                    newClassMember.PersonId  = updatedPerson.Id;
                    newClassMember.ClassRole = ClassRoles.Member;
                    InsertClassMember(newClassMember);

                    logger.DebugFormat("Person [{0} {1}] was added.",
                                       updatedPerson.FirstName, updatedPerson.LastName);
                }
                else
                {
                    logger.Debug("Person is being updated.");

                    CACCCheckInDb.People.UpdatePerson(updatedPerson);

                    logger.DebugFormat("Person [{0} {1}] was updated.",
                                       updatedPerson.FirstName, updatedPerson.LastName);
                }
            }
            catch (Exception ex)
            {
                Debug.Assert(View != null);
                View.ViewDispatcher.BeginInvoke(DispatcherPriority.DataBind,
                                                new DispatcherOperationCallback(
                                                    delegate(object arg)
                {
                    View.DisplayExceptionDetail(ex);
                    return(null);
                }), null);
            }
        }
        /// <summary>
        /// Whenever people are added or removed from the collection,
        /// we will get notified here and can handle the event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void PeopleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            try
            {
                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    // A new Person has started being added to the People list.
                    // We will set some default values and hook up some
                    // event handler so we can capture if they
                    // actually save the new Person.
                    foreach (CACCCheckInDb.Person person in e.NewItems)
                    {
                        CACCCheckInDb.Person thePerson = (CACCCheckInDb.Person)person;
                        person.Id           = Guid.NewGuid();
                        person.FirstName    = "<Enter First Name>";
                        person.LastName     = "<Enter Last Name>";
                        person.FamilyId     = Guid.NewGuid();
                        person.PhoneNumber  = String.Empty;
                        person.ItemEndEdit +=
                            new CACCCheckInDb.ItemEndEditEventHandler(PeopleItemEndEdit);
                    }
                    break;

                case NotifyCollectionChangedAction.Move:
                    break;

                case NotifyCollectionChangedAction.Remove:
                    // People have been removed from the People list.
                    // We will delete the Person.
                    foreach (CACCCheckInDb.Person person in e.OldItems)
                    {
                        logger.DebugFormat("Deleting person [{0} {1}]",
                                           person.FirstName, person.LastName);

                        CACCCheckInDb.People.DeletePerson(person);
                    }
                    break;

                case NotifyCollectionChangedAction.Replace:
                    break;

                case NotifyCollectionChangedAction.Reset:
                    break;
                }
            }
            catch (Exception ex)
            {
                Debug.Assert(View != null);
                View.ViewDispatcher.BeginInvoke(DispatcherPriority.DataBind,
                                                new DispatcherOperationCallback(
                                                    delegate(object arg)
                {
                    View.DisplayExceptionDetail(ex);
                    return(null);
                }), null);
            }
        }
        public CACCCheckInDb.Person InsertPeople(CACCCheckInDb.Person person)
        {
            logger.Debug("Opening DataContext to CACCCheckIn DB.");
            CACCCheckInDb.CACCCheckInDbDataContext db = new CACCCheckInDb.CACCCheckInDbDataContext();
            db.Log = logwriter;

            logger.DebugFormat("Inserting Person: [{0} {1}]", person.FirstName,
                               person.LastName);
            db.Persons.InsertOnSubmit(person);

            db.SubmitChanges();

            return(db.Persons.Where(p =>
                                    p.Id.Equals(person.Id)).Single <CACCCheckInDb.Person>());
        }