public void Add(Profile profile)
        {
            var userData = profile.UserData.CreateUserModelInstance();

            base.Users.Add(userData);
            SaveChanges();
            var userId = userData.ID;

            if (profile.UserResponses != null && profile.UserResponses.Count() > 0)
            {
                AddToResponses(QuestionTypeEnum.UserProfile, userId, profile.UserResponses);
            }

            if (profile.MatchResponses != null && profile.MatchResponses.Count() > 0)
            {
                AddToResponses(QuestionTypeEnum.MatchPreference, userId, profile.MatchResponses);
            }

            if (profile.ChoiceResponses != null && profile.ChoiceResponses.Count() > 0)
            {
                AddToChoices(userId, profile.ChoiceResponses);
            }

            SaveChanges();
        }
        public void Attach(Profile profile)
        {
            var userData = profile.UserData.CreateUserModelInstance(profile.UserData.ProlieUserData.UserID.Value);
            base.Users.Attach(userData);
            userData = profile.UserData.SetUserModelInstance(userData);

            var isModified = base.Entry(userData).Property(u => u.DisplayName).IsModified;

            if (profile.UserResponses != null && profile.UserResponses.Count() > 0)
            {
                // For the user response table given all the possibilities of checking and unchecking check boxes
                // and selecting No Preferences which automatically deselects everything else, easier to delete all
                // first and then do an add.

                long questionTypeId = QuestionTypeID(QuestionTypeEnum.UserProfile.ToString()).Value;

                // Need to remove all the records for user responses before we do the add.
                base.UserResponses.RemoveRange(base.UserResponses.Where(x => x.QuestionTypeID == questionTypeId &&
                                                                             x.UserID == userData.ID));

                AddToResponses(QuestionTypeEnum.UserProfile, userData.ID, profile.UserResponses);

            }

            if (profile.MatchResponses != null && profile.MatchResponses.Count() > 0)
            {
                // For the user response table given all the possibilities of checking and unchecking check boxes
                // and selecting No Preferences which automatically deselects everything else, easier to delete all
                // first and then to an add.

                long questionTypeId = QuestionTypeID(QuestionTypeEnum.MatchPreference.ToString()).Value;

                // Need to remove all the records for match responses before we do the add.
                base.UserResponses.RemoveRange(base.UserResponses.Where(x => x.QuestionTypeID == questionTypeId &&
                                                                             x.UserID == userData.ID));

                AddToResponses(QuestionTypeEnum.MatchPreference, userData.ID, profile.MatchResponses);

            }

            if (profile.ChoiceResponses != null && profile.ChoiceResponses.Count() > 0)
            {
                UpdateChoices(userData.ID, profile.ChoiceResponses);

            }

            SaveChanges();
        }
        public Profile GetDataContractByUser(string key, 
                                             QuestionTypeEnum questionType)
        {
            Profile userProfile = new Profile();
            long? userProfileQuestionTypeId = null;
            long? matchProfileQuestionTypeId = null;

            switch (questionType)
            {
                case QuestionTypeEnum.UserProfile:
                    {
                        // Get question tyoe id for adding user responses
                        userProfileQuestionTypeId = QuestionTypeID(QuestionTypeEnum.UserProfile.ToString());
                        var result = (from user in base.Users
                                      where user.UserName == key
                                      select new Profile
                                      {

                                          UserData = new ProfileUser
                                          {
                                              User = user
                                          },

                                          UserResponses = (from resp in user.UserResponses
                                                           where resp.QuestionTypeID == userProfileQuestionTypeId
                                                           select new ProfileResponse
                                                           {
                                                               Response = resp
                                                           }
                                                           ).ToList()

                                      }
                         );

                         userProfile = result.SingleOrDefault();
                    }
                    break;
                case QuestionTypeEnum.MatchPreference:
                    {
                        matchProfileQuestionTypeId = QuestionTypeID(QuestionTypeEnum.MatchPreference.ToString());

                        var result = (from user in base.Users
                                      where user.UserName == key
                                      select new Profile
                                      {

                                          UserData = new ProfileUser
                                          {
                                              User = user
                                          },

                                          MatchResponses = (from resp in user.UserResponses
                                                            where resp.QuestionTypeID == matchProfileQuestionTypeId
                                                            select new ProfileResponse
                                                            {
                                                                Response = resp
                                                            }
                                                           ).ToList(),

                                      }
                         );

                        userProfile = result.SingleOrDefault();
                    }
                    break;
                case QuestionTypeEnum.ChoiceResponses:
                    {
                        var result = (from user in base.Users
                                      where user.UserName == key
                                      select new Profile
                                      {

                                          UserData = new ProfileUser
                                          {
                                              User = user
                                          },

                                          ChoiceResponses = (from choice in user.ProfileChocies
                                                             join puser in Users
                                                             on choice.ProfileChoiceUserID equals puser.ID
                                                             join choiceType in Ref_ChoiceType
                                                             on choice.ChoiceType equals choiceType.ID
                                                             where choice.UserID == user.ID
                                                             select new Choice
                                                             {
                                                                 UserChoice = choice,
                                                                 MUser = puser,
                                                                 ChoiceTypeStr = choiceType.MatchType

                                                             }
                                                           ).ToList()

                                      }
                         );

                        userProfile = result.SingleOrDefault();
                    }
                    break;
                case QuestionTypeEnum.All:
                    {
                        // Get question tyoe id for adding user responses
                        //userProfileQuestionTypeId = QuestionTypeID(questionType.ToString());
                        userProfileQuestionTypeId = QuestionTypeID(QuestionTypeEnum.UserProfile.ToString());
                        matchProfileQuestionTypeId = QuestionTypeID(QuestionTypeEnum.MatchPreference.ToString());

                        var result = (from user in base.Users
                                      where user.UserName == key
                                      select new Profile
                                      {

                                          UserData = new ProfileUser
                                          {
                                              User = user
                                          },

                                          UserResponses = (from resp in user.UserResponses
                                                           where resp.QuestionTypeID == userProfileQuestionTypeId
                                                           select new ProfileResponse
                                                           {
                                                               Response = resp
                                                           }
                                                           ).ToList(),

                                          MatchResponses = (from resp in user.UserResponses
                                                            where resp.QuestionTypeID == matchProfileQuestionTypeId
                                                            select new ProfileResponse
                                                            {
                                                                Response = resp
                                                            }
                                                           ).ToList(),

                                          ChoiceResponses = (from choice in user.ProfileChocies
                                                             join puser in Users
                                                             on choice.ProfileChoiceUserID equals puser.ID
                                                             join choiceType in Ref_ChoiceType
                                                             on choice.ChoiceType equals choiceType.ID
                                                             where choice.UserID == user.ID
                                                             select new Choice
                                                             {
                                                                 UserChoice = choice,
                                                                 MUser = puser,
                                                                 ChoiceTypeStr = choiceType.MatchType

                                                             }
                                                           ).ToList()

                                      }
                         );

                        userProfile = result.SingleOrDefault();
                    }
                    break;
                case QuestionTypeEnum.BothProfiles:
                    {
                        // Get question tyoe id for adding user responses
                        //userProfileQuestionTypeId = QuestionTypeID(questionType.ToString());
                        userProfileQuestionTypeId = QuestionTypeID(QuestionTypeEnum.UserProfile.ToString());
                        matchProfileQuestionTypeId = QuestionTypeID(QuestionTypeEnum.MatchPreference.ToString());

                        var result = (from user in base.Users
                                      where user.UserName == key
                                      select new Profile
                                      {

                                          UserData = new ProfileUser
                                          {
                                              User = user
                                          },

                                          UserResponses = (from resp in user.UserResponses
                                                           where resp.QuestionTypeID == userProfileQuestionTypeId
                                                           select new ProfileResponse
                                                           {
                                                               Response = resp
                                                           }
                                                           ).ToList(),

                                          MatchResponses = (from resp in user.UserResponses
                                                            where resp.QuestionTypeID == matchProfileQuestionTypeId
                                                            select new ProfileResponse
                                                            {
                                                                Response = resp
                                                            }
                                                           ).ToList(),

                                      }
                         );

                        userProfile = result.SingleOrDefault();
                    }
                    break;
                case QuestionTypeEnum.None:
                    {
                        var result = (from user in base.Users
                                      where user.UserName == key
                                      select new Profile
                                      {
                                          UserData = new ProfileUser
                                          {
                                              User = user
                                          },
                                      }
                                      );
                        userProfile = result.SingleOrDefault();
                    }
                    break;
            }

            return userProfile;
        }
        public bool DoesProfileMatch(Profile userProfile,
                                     Profile matchProfile)
        {
            /*
            foreach (ProfileResponse match in userProfile.MatchResponses)
            {
                if (matchProfile.UserResponses != null && matchProfile.UserResponses.Count() > 0)
                {
                    //

                }
            }
             */

            return true;
        }