예제 #1
0
        public bool UpdateUserProfile(Person updatedPerson)
        {
            try
            {
                var regContext = new RegistrationDbContext();

                //get existing person
                var person = _contextProvider.Context.People.Include(x => x.Contact).Include(x=> x.Address).SingleOrDefault(p => p.Id == updatedPerson.Id);

                #region update meta data properties
                person.LastChangeDate = DateTime.UtcNow;
                person.Address.LastChangeDate = DateTime.UtcNow;
                if (person.HomeAddress != null)
                    person.HomeAddress.LastChangeDate = DateTime.UtcNow;
                person.Contact.LastChangeDate = DateTime.UtcNow;
                person.LastChangePersonId = person.Id;
                person.Address.LastChangePersonId = person.Id;
                if (person.HomeAddress != null)
                   person.HomeAddress.LastChangePersonId = person.Id;
                person.Contact.LastChangePersonId = person.Id;
                #endregion  

                #region update person details
                person.Salutation = updatedPerson.Salutation;
                person.FirstName = updatedPerson.FirstName;
                person.LastName = updatedPerson.LastName;
                person.MiddleName = updatedPerson.MiddleName;
                person.Suffix = updatedPerson.Suffix;
                person.Title = updatedPerson.Title;

                person.Contact.Phone = updatedPerson.Contact.Phone;
                person.Contact.Fax = updatedPerson.Contact.Fax;
                person.Contact.Cell = updatedPerson.Contact.Cell;

                person.Address.AddressLine1 = updatedPerson.Address.AddressLine1;
                person.Address.AddressLine2 = updatedPerson.Address.AddressLine2;
                person.Address.CityLocality = updatedPerson.Address.CityLocality;
                person.Address.StateProvince = updatedPerson.Address.StateProvince;
                person.Address.Country = updatedPerson.Address.Country;
                person.Address.PostalCode = updatedPerson.Address.PostalCode;

                #endregion 

                regContext.People.Attach((person));
                regContext.Entry(person).State = System.Data.Entity.EntityState.Modified;

                if (person.Address != null)
                    regContext.Entry(person.Address).State = System.Data.Entity.EntityState.Modified;
                if (person.HomeAddress != null)
                    regContext.Entry(person.HomeAddress).State = System.Data.Entity.EntityState.Modified;
                if (person.Contact != null)
                    regContext.Entry(person.Contact).State = System.Data.Entity.EntityState.Modified;
                regContext.SaveChanges();

                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
예제 #2
0
        /// <summary>
        /// Inserts a new record inot the People table
        /// </summary>
        /// <param name="person"></param>
        /// <returns></returns>
        public bool CreatePerson(Person person)
        {
            try
            {

                #region Setup metadata properties

                //Set meta data properties.
                person.UniqueId = Guid.NewGuid();
                person.CreatedDate = DateTime.UtcNow;
                person.LastChangeDate = DateTime.UtcNow;

                person.Address.UniqueId = Guid.NewGuid();
                person.Address.CreatedDate = DateTime.UtcNow;
                person.Address.LastChangeDate = DateTime.UtcNow;

                if (person.HomeAddress != null)
                {
                    person.HomeAddress.UniqueId = Guid.NewGuid();
                    person.HomeAddress.CreatedDate = DateTime.UtcNow;
                    person.HomeAddress.LastChangeDate = DateTime.UtcNow;
                }

                person.Contact.UniqueId = Guid.NewGuid();
                person.Contact.CreatedDate = DateTime.UtcNow;
                person.Contact.LastChangeDate = DateTime.UtcNow;

                //Temporarily set unknown values.
                person.Address.CreatedPersonId = 1;
                person.Address.LastChangePersonId = 1;
                if (person.HomeAddress != null)
                {
                    person.HomeAddress.CreatedPersonId = 1;
                    person.HomeAddress.LastChangePersonId = 1;
                }
                person.Contact.CreatedPersonId = 1;
                person.Contact.LastChangePersonId = 1;

                #endregion

                var regContext = new RegistrationDbContext();
                regContext.People.Add((person));
                regContext.SaveChanges();

                #region Update Model

                person.ContactId = person.Contact.Id;
                person.AddressId = person.Address.Id;
                if (person.HomeAddress != null)
                {
                    person.HomeAddressId = person.HomeAddress.Id;
                }

                //Update the person record with the correct created and last change person Ids.
                person.CreatedPersonId = person.Id;
                person.LastChangePersonId = person.Id;
                person.Address.CreatedPersonId = person.Id;
                person.Address.LastChangePersonId = person.Id;
                if (person.HomeAddress != null)
                {
                    person.HomeAddress.CreatedPersonId = person.Id;
                    person.HomeAddress.LastChangePersonId = person.Id;
                }
                person.Contact.CreatedPersonId = person.Id;
                person.Contact.LastChangePersonId = person.Id;

                #endregion

                regContext.People.Attach((person));
                regContext.Entry(person).State = System.Data.Entity.EntityState.Modified;

                if (person.Address != null)
                    regContext.Entry(person.Address).State = System.Data.Entity.EntityState.Modified;
                if (person.HomeAddress != null)
                    regContext.Entry(person.HomeAddress).State = System.Data.Entity.EntityState.Modified;
                if (person.Contact != null)
                    regContext.Entry(person.Contact).State = System.Data.Entity.EntityState.Modified;
                regContext.SaveChanges();

                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }