public async Task<int> CreateAsync(User user)
 {
     using (var db = new UserContext())
     {
         var addedUser = db.Users.Add(user);
         db.SaveChanges();
         return addedUser.UserId;
     }
 }
        public async Task<bool> UpdateAsync(User user)
        {
            using (var db = new UserContext())
            {
                var result = await db.Users.SingleOrDefaultAsync(e => e.UserId == user.UserId);
                if (result != null)
                {
                    result.Name = user.Name;
                    await db.SaveChangesAsync();

                    return true;
                }

                return false;
            }
        }
Example #3
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            using (var db = new UsersData())
            {
                for (int i = 0; i < amount; i++)
                {
                    var user = new User { Name = this.dataGridViewUsersInfo.Rows[i].Cells[0].Value.ToString(), Post = this.dataGridViewUsersInfo.Rows[i].Cells[1].Value.ToString() };
                    db.Users.Add(user);
                    db.SaveChanges();
                    string[] arr = this.dataGridViewUsersInfo.Rows[i].Cells[2].Value.ToString().Split(new char[] {' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

                    for (int j = 0; j < arr.Length; j++)
                    {
                        var temp = arr[j];
                        var query = (from c in db.Certificates
                                    where c.Name == temp
                                    select c.ID).ToList()[0];

                        var relation = new Relation { UserID = user.ID, CertificateID = query};
                        db.Relations.Add(relation);
                        db.SaveChanges();
                    }
                }
            }
            this.dataGridViewUsersInfo.Rows.Clear();
            this.dataGridViewUsersInfo.Columns.Clear();
            DataGridInit();
        }