public int CreateBusiness(string name, string regNumber)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Business business = new Business();
                    business.Name      = name;
                    business.RegNumber = regNumber;
                    business.Type      = "B";

                    context.Parties.AddObject(business);
                    context.SaveChanges();

                    return(business.Id);
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void UpdatePerson(int id, string firstName, string lastName, DateTime dateOfBirth)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Person person = (Person)
                                    (from p in context.Parties
                                     where p.Id == id
                                     select p).First();

                    if (person != null)
                    {
                        person.FirstName   = firstName;
                        person.LastName    = lastName;
                        person.DateOfBirth = dateOfBirth;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void DeleteUser(string name)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    User user =
                        (from u in context.Users
                         where (u.Name == name)
                         select u).First();

                    if (user != null)
                    {
                        context.DeleteObject(user);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void UpdateBusiness(int id, string name, string regNumber)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    var business = (Business)
                                   (from b in context.Parties
                                    where b.Id == id
                                    select b).First();

                    if (business != null)
                    {
                        business.Name      = name;
                        business.RegNumber = regNumber;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public int CreateAddress(string street, string town, string county, string postCode)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Address address = new Address();
                    address.Street   = street;
                    address.Town     = town;
                    address.County   = county;
                    address.PostCode = postCode;
                    address.Type     = "A";

                    context.Contacts.AddObject(address);
                    context.SaveChanges();
                    return(address.Id);
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void UpdateUser(string name, string fullName, string passwordHash)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    User user = (User)
                                (from u in context.Users
                                 where u.Name == name
                                 select u).First();

                    if (user != null)
                    {
                        user.FullName     = fullName;
                        user.PasswordHash = passwordHash;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void UpdateAddress(int id, string street, string town, string county, string postCode)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Address address = (Address)
                                      (from a in context.Contacts
                                       where a.Id == id
                                       select a).First();

                    if (address != null)
                    {
                        address.Street   = street;
                        address.Town     = town;
                        address.County   = county;
                        address.PostCode = postCode;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void UpdateEmail(int id, string type, string address)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Email email = (Email)
                                  (from e in context.Contacts
                                   where e.Id == id
                                   select e).First();

                    if (email != null)
                    {
                        email.Address = address;
                        email.SubType = type;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public int CreateEmail(string type, string address)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Email email = new Email();
                    email.Address = address;
                    email.SubType = type;
                    email.Type    = "E";

                    context.Contacts.AddObject(email);
                    context.SaveChanges();
                    return(email.Id);
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void UpdateTelephone(int id, string type, string number)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Telephone telephone = (Telephone)
                                          (from t in context.Contacts
                                           where t.Id == id
                                           select t).First();

                    if (telephone != null)
                    {
                        telephone.Number  = number;
                        telephone.SubType = type;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public int CreateTelephone(string type, string number)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Telephone telephone = new Telephone();
                    telephone.Number  = number;
                    telephone.SubType = type;
                    telephone.Type    = "T";

                    context.Contacts.AddObject(telephone);
                    context.SaveChanges();
                    return(telephone.Id);
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void CreatePartyContact(int partyId, int contactId, DateTime validFrom, DateTime validUntil)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    PartyContact partyContact = new PartyContact();
                    partyContact.PartyId    = partyId;
                    partyContact.ContactId  = contactId;
                    partyContact.ValidFrom  = validFrom;
                    partyContact.ValidUntil = validUntil;

                    context.PartyContacts.AddObject(partyContact);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void DeleteContact(int id)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Contact contact =
                        (from ct in context.Contacts
                         where (ct.Id == id)
                         select ct).First();

                    if (contact != null)
                    {
                        context.DeleteObject(contact);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void UpdatePartyContact(int partyId, int contactId, DateTime validFrom, DateTime validUntil)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    PartyContact partyContact = (PartyContact)
                                                (from pc in context.PartyContacts
                                                 where (pc.PartyId == partyId && pc.ContactId == contactId)
                                                 select pc).First();

                    if (partyContact != null)
                    {
                        partyContact.ValidFrom  = validFrom;
                        partyContact.ValidUntil = validUntil;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void DeletePartyContact(int partyId, int contactId)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    PartyContact partyContact =
                        (from pc in context.PartyContacts
                         where (pc.PartyId == partyId && pc.ContactId == contactId)
                         select pc).First();

                    if (partyContact != null)
                    {
                        context.DeleteObject(partyContact);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public int CreatePerson(string firstName, string lastName, DateTime dateOfBirth)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Person person = new Person();
                    person.FirstName   = firstName;
                    person.LastName    = lastName;
                    person.DateOfBirth = dateOfBirth;
                    person.Type        = "P";

                    context.Parties.AddObject(person);
                    context.SaveChanges();

                    return(person.Id);
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public void DeleteParty(int id)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    using (TransactionScope txnScope = new TransactionScope(TransactionScopeOption.Required))
                    {
                        Party party =
                            (from p in context.Parties
                             where (p.Id == id)
                             select p).First();

                        if (party != null)
                        {
                            var partyContacts =
                                from partyContact in context.PartyContacts
                                where (partyContact.PartyId == id)
                                select partyContact;

                            foreach (PartyContact toRemove in partyContacts)
                            {
                                context.DeleteObject(toRemove);
                                context.SaveChanges();
                            }

                            context.DeleteObject(party);

                            context.SaveChanges();
                            context.AcceptAllChanges();
                            txnScope.Complete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public int CreateEmail(string type, string address)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Email email = new Email();
                    email.Address = address;
                    email.SubType = type;
                    email.Type = "E";

                    context.Contacts.AddObject(email);
                    context.SaveChanges();
                    return email.Id;
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public int CreateBusiness(string name, string regNumber)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Business business = new Business();
                    business.Name = name;
                    business.RegNumber = regNumber;
                    business.Type = "B";

                    context.Parties.AddObject(business);
                    context.SaveChanges();

                    return business.Id;
                }

            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void CreateUser(string name, string fullName, string passwordHash)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    User user = new User();
                    user.Name         = name;
                    user.FullName     = fullName;
                    user.PasswordHash = passwordHash;

                    context.Users.AddObject(user);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
        public int CreateAddress(string street, string town, string county, string postCode)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Address address = new Address();
                    address.Street = street;
                    address.Town = town;
                    address.County = county;
                    address.PostCode = postCode;
                    address.Type = "A";

                    context.Contacts.AddObject(address);
                    context.SaveChanges();
                    return address.Id;
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void DeleteUser(string name)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {

                    User user =
                        (from u in context.Users
                         where (u.Name == name)
                         select u).First();

                    if (user != null)
                    {
                        context.DeleteObject(user);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void DeletePartyContact(int partyId, int contactId)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {

                    PartyContact partyContact =
                       (from pc in context.PartyContacts
                        where (pc.PartyId == partyId && pc.ContactId == contactId)
                        select pc).First();

                    if (partyContact != null)
                    {
                        context.DeleteObject(partyContact);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void UpdateBusiness(int id, string name, string regNumber)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    var business = (Business)
                        (from b in context.Parties
                         where b.Id == id
                         select b).First();

                    if (business != null)
                    {
                        business.Name = name;
                        business.RegNumber = regNumber;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void UpdateAddress(int id, string street, string town, string county, string postCode)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Address address = (Address)
                        (from a in context.Contacts
                         where a.Id == id
                         select a).First();

                    if (address != null)
                    {
                        address.Street = street;
                        address.Town = town;
                        address.County = county;
                        address.PostCode = postCode;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void UpdatePartyContact(int partyId, int contactId, DateTime validFrom, DateTime validUntil)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    PartyContact partyContact = (PartyContact)
                        (from pc in context.PartyContacts
                         where (pc.PartyId == partyId && pc.ContactId == contactId)
                         select pc).First();

                    if (partyContact != null)
                    {
                        partyContact.ValidFrom = validFrom;
                        partyContact.ValidUntil = validUntil;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void UpdateEmail(int id, string type, string address)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {

                    Email email = (Email)
                        (from e in context.Contacts
                         where e.Id == id
                         select e).First();

                    if (email != null)
                    {
                        email.Address = address;
                        email.SubType = type;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void UpdatePerson(int id, string firstName, string lastName, DateTime dateOfBirth)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Person person = (Person)
                        (from p in context.Parties
                         where p.Id == id
                         select p).First();

                    if (person != null)
                    {
                        person.FirstName = firstName;
                        person.LastName = lastName;
                        person.DateOfBirth = dateOfBirth;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void UpdateTelephone(int id, string type, string number)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Telephone telephone = (Telephone)
                        (from t in context.Contacts
                         where t.Id == id
                         select t).First();

                    if (telephone != null)
                    {
                        telephone.Number = number;
                        telephone.SubType = type;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void UpdateUser(string name, string fullName, string passwordHash)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    User user = (User)
                        (from u in context.Users
                         where u.Name == name
                         select u).First();

                    if (user != null)
                    {
                        user.FullName = fullName;
                        user.PasswordHash = passwordHash;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void CreatePartyContact(int partyId, int contactId, DateTime validFrom, DateTime validUntil)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    PartyContact partyContact = new PartyContact();
                    partyContact.PartyId = partyId;
                    partyContact.ContactId = contactId;
                    partyContact.ValidFrom = validFrom;
                    partyContact.ValidUntil = validUntil;

                    context.PartyContacts.AddObject(partyContact);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void DeleteParty(int id)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    using (TransactionScope txnScope = new TransactionScope(TransactionScopeOption.Required))
                    {
                        Party party =
                            (from p in context.Parties
                             where (p.Id == id)
                             select p).First();

                        if (party != null)
                        {
                            var partyContacts =
                                from partyContact in context.PartyContacts
                                where (partyContact.PartyId == id)
                                select partyContact;

                            foreach (PartyContact toRemove in partyContacts)
                            {
                                context.DeleteObject(toRemove);
                                context.SaveChanges();
                            }

                            context.DeleteObject(party);

                            context.SaveChanges();
                            context.AcceptAllChanges();
                            txnScope.Complete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public int CreateTelephone(string type, string number)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Telephone telephone = new Telephone();
                    telephone.Number = number;
                    telephone.SubType = type;
                    telephone.Type = "T";

                    context.Contacts.AddObject(telephone);
                    context.SaveChanges();
                    return telephone.Id;
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void DeleteContact(int id)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {

                    Contact contact =
                        (from ct in context.Contacts
                         where (ct.Id == id)
                         select ct).First();

                    if (contact != null)
                    {
                        context.DeleteObject(contact);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public int CreatePerson(string firstName, string lastName, DateTime dateOfBirth)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    Person person = new Person();
                    person.FirstName = firstName;
                    person.LastName = lastName;
                    person.DateOfBirth = dateOfBirth;
                    person.Type = "P";

                    context.Parties.AddObject(person);
                    context.SaveChanges();

                    return person.Id;
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }
        public void CreateUser(string name, string fullName, string passwordHash)
        {
            try
            {
                using (PartyEntities context = new PartyEntities(this.connectionString))
                {
                    User user = new User();
                    user.Name = name;
                    user.FullName = fullName;
                    user.PasswordHash = passwordHash;

                    context.Users.AddObject(user);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                if (ex is UpdateException) { throw ex.InnerException; }
                throw;
            }
        }