private void UpdateOperations()
        {
            var syncInfo = new SyncInfo();

            using (var context = new PhoneDexContext())
            {
                bool isUpdated = false;

                if (context.SyncInfo.FirstOrDefault() != null)
                {
                    syncInfo = context.SyncInfo.First();
                }

                if (syncInfo.lastLdapSyncDate != null)
                {
                    isUpdated = IsUpToDate(DateTime.Now, syncInfo.lastLdapSyncDate.Value);
                }

                if (isUpdated == false)
                {
                    var integratedContacts = new LdapServiceManager().getAllUsers();
                    var manualContacts     = context.Contacts.Where(x => x.isManual);

                    if (integratedContacts.Any())
                    {
                        DeleteIntegratedUsers(context);
                    }

                    _contactList.AddRange(manualContacts);
                    _contactList.AddRange(integratedContacts);
                    context.Contacts.AddRange(integratedContacts);

                    context.SyncInfo.RemoveRange(context.SyncInfo);
                    context.SaveChanges();

                    syncInfo.recordsCount     = integratedContacts.Count;
                    syncInfo.lastLdapSyncDate = DateTime.Now;
                    context.SyncInfo.Add(syncInfo);
                    context.SaveChanges();
                }
                else
                {
                    foreach (var contact in (from r in context.Contacts select r))
                    {
                        _contactList.Add(contact);
                    }
                }
            }
        }
        public void SaveContact(ContactDto contactDto)
        {
            using (var context = new PhoneDexContext())
            {
                //new user
                if (contactDto.IsNewUser())
                {
                    var newContact = new Contact
                    {
                        givenName   = contactDto.givenName,
                        sn          = contactDto.sn,
                        displayName = contactDto.givenName + " " + contactDto.sn,
                        title       = contactDto.title,
                        cn          = contactDto.givenName + " " + contactDto.sn,
                        physicalDeliveryOfficeName = contactDto.physicalDeliveryOfficeName,
                        mail            = contactDto.mail,
                        mobile          = contactDto.mobile,
                        telephoneNumber = contactDto.telephoneNumber,
                        isManual        = true,
                        whenCreated     = DateTime.Now
                    };

                    context.Contacts.Add(newContact);
                    context.SaveChanges();
                    _contactList.Add(newContact);
                }
                else
                {
                    ///update

                    var contactInDatabase = context.Contacts.Single(contact => contact.Id == contactDto.Id);
                    contactInDatabase.givenName   = contactDto.givenName;
                    contactInDatabase.sn          = contactDto.sn;
                    contactInDatabase.displayName = contactDto.givenName + " " + contactDto.sn;
                    contactInDatabase.cn          = contactDto.givenName + " " + contactDto.sn;
                    contactInDatabase.title       = contactDto.title;
                    contactInDatabase.physicalDeliveryOfficeName = contactDto.physicalDeliveryOfficeName;
                    contactInDatabase.mail            = contactDto.mail;
                    contactInDatabase.mobile          = contactDto.mobile;
                    contactInDatabase.telephoneNumber = contactDto.telephoneNumber;
                    contactInDatabase.whenChanged     = DateTime.Now;

                    context.SaveChanges();
                }
            }
        }
 public JsonResult DeleteContact(Contact deletedContact)
 {
     using (var context = new PhoneDexContext())
     {
         var tempContact = context.Contacts.SingleOrDefault(x => x.Id == deletedContact.Id);
         if (tempContact != null)
         {
             _contactList.RemoveAll(c => c.Id == tempContact.Id);
             context.Contacts.Remove(tempContact);
             context.SaveChanges();
         }
     }
     return(Json(_contactList));
 }
 private void DeleteIntegratedUsers(PhoneDexContext context)
 {
     context.Contacts.RemoveRange(context.Contacts.Where(i => !i.isManual));
     context.SaveChanges();
 }