public void UpdateContact(int clientId, int contactTypeId, string value) { var curContact = (from Contact item in db.Contacts where item.ClientID == clientId && item.ContactTypeID == contactTypeId && item.Active select item).FirstOrDefault(); if (curContact == null) { curContact = new Contact() { Active=true, ClientID=clientId, ContactTypeID = contactTypeId }; db.Contacts.Add(curContact); } curContact.ContactName = value.Replace(" ", string.Empty); db.SaveChanges(); }
private int CreateContact(int clientId, string contactType, string contactValue) { var active = true; if (!(contactValue?.Length > 0)) { contactValue = "None"; active = false; } string errorMessage = string.Format("Contact type \"{0}\" not defined in lookup", contactType); var type = db.ContactTypes.FirstOrDefault(m => m.ContactTypeName == contactType); if (type == null) throw new Exception(errorMessage); int oldContactActivatedId = 0; var checkExisting = (from item in db.Contacts where item.ContactTypeID == type.ContactTypeId && item.ClientID == clientId select item).ToArray(); for (int i=0;i< checkExisting.Length; i++) { if (checkExisting[i].ContactName.ToLower() == contactValue.ToLower()) { checkExisting[i].Active = true; oldContactActivatedId = checkExisting[i].ContactId; } else { checkExisting[i].Active = false; } } if(checkExisting.Length>0) db.SaveChanges(); if (oldContactActivatedId > 0) return oldContactActivatedId; var newContact = new Contact() { Active = active, ClientID = clientId, ContactName = contactValue, ContactTypeID = type.ContactTypeId }; db.Contacts.Add(newContact); db.SaveChanges(); return newContact.ContactId; }