Ejemplo n.º 1
0
        private void SaveChanges_button_Click(object sender, EventArgs e)
        {
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException exception)
            {
                MessageBox.Show(exception.Message);
            }

            MessageBox.Show("Users added.");
        }
Ejemplo n.º 2
0
        private void SaveChanges()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            MessageBox.Show("Changes saved.");
        }
Ejemplo n.º 3
0
 public static void CreateNewUsers(CustomerTableAdapter customerTableAdapter,
                                   ref BindingSource bindingSource, ref IdeallyConnectedTestDbEntities context)
 {
     // Iterating through the list of users from AdventureWorksLT2008
     foreach (DataRow customer in customerTableAdapter.GetData().Rows)
     {
         Users newUser = new Users();
         newUser.Id          = Guid.NewGuid().ToString();
         newUser.FirstName   = (string)customer["FirstName"];
         newUser.LastName    = (string)customer["LastName"];
         newUser.UserName    = newUser.FirstName + newUser.LastName + Convert.ToString(random.Next()).Substring(0, 5);
         newUser.PhoneNumber = (string)customer["Phone"];
         newUser.Email       = (string)customer["EmailAddress"];
         newUser.Created     = DateTime.Now;
         context.Users.Add(newUser);
         if (bindingSource.Count % 100 == 0)
         {
             context.SaveChanges();
         }
         bindingSource.Add(newUser);
     }
 }
        /// <summary>
        /// Create collaborations amongst the selected users. Currently this can only generate 2 collaborations
        /// for 1/3 of the users selected. The users must be selected contiguosly.
        /// </summary>
        /// <param name="_context">The dbcontext where the database will be persisted.</param>
        /// <param name="bindingUsers">All the users on the data grid.</param>
        /// <param name="UsersDataGrid">The data grid.</param>
        public static void CreateCollaboratorsFromSelected(IdeallyConnectedTestDbEntities _context,
                                                           IList <Users> bindingUsers, DataGridView UsersDataGrid)
        {
            // Store the indexes representing the rows selected
            int totalRowsSelected = UsersDataGrid.SelectedRows.Count;

            // Storing all the existing Collaborators into a dictionary to prevent adding existing data.
            _context.Collaborators.Load();
            Dictionary <string, Dictionary <string, bool> > collabChecker =
                new Dictionary <string, Dictionary <string, bool> >(_context.Collaborators.Count());

            foreach (Collaborators co in _context.Collaborators.Local)
            {
                if (!collabChecker.ContainsKey(co.UserA))
                {
                    collabChecker.Add(co.UserA, new Dictionary <string, bool>()
                    {
                        { co.UserB, true }
                    });
                }
                else
                {
                    collabChecker[co.UserA].Add(co.UserB, true);
                }
            }

            _context.Configuration.AutoDetectChangesEnabled = false; // improves performance

            // Create a collaboration between a user and the two (rows) after.
            for (int i = 0; i < totalRowsSelected; ++i)
            {
                int j             = i + 1;
                int userdataGridi = UsersDataGrid["userNameDataGridViewTextBoxColumn", i].ColumnIndex;

                // Make the next two users as collaborators with the user, i.
                for (; j < totalRowsSelected && j < i + 3 && j < UsersDataGrid.RowCount - 2; ++j)
                {
                    // Check if the collaberators are already stored in the database; save changes periodically. No change checking.
                    if (!collabChecker.ContainsKey(bindingUsers[i].Id) ||
                        !collabChecker[bindingUsers[i].Id].ContainsKey(bindingUsers[j].Id))
                    {
                        // Create and add a new collaborating user
                        _context.Collaborators.Add(new Collaborators()
                        {
                            UserA                = bindingUsers[i].Id,
                            UserB                = bindingUsers[j].Id,
                            Following            = true,
                            InitialCollaboration = DateTime.Now,
                            Initiated            = true
                        });

                        if (_context.Collaborators.Count() % 100 == 0)
                        {
                            _context.SaveChanges();
                        }
                    }
                }

                i = j - 1;
            }

            _context.Configuration.AutoDetectChangesEnabled = true;
        }