Example #1
0
        public void Update(int id, TEntity obj)
        {
            TEntity entity = GetById(id);

            if (entity != null)
            {
                _context.Entry(entity).CurrentValues.SetValues(obj);
            }

            _context.Entry(entity).State = EntityState.Modified;
            _context.SaveChanges();
        }
        private void SaveRecordButton_Click(object sender, EventArgs e)
        {
            Student student = new Student();

            student.StudentId = StudentId;
            student.FullName  = FullNameTextBox.Text;
            student.Email     = EmailTextBox.Text;
            student.Mobile    = MobileTextBox.Text;
            student.Phone     = PhoneTextBox.Text;
            student.Gender    = GetGender();
            student.CSharp    = CSharpCheckBox.Checked;
            student.VBNET     = VBNETCheckBox.Checked;
            student.HTML      = HtmlCheckBox.Checked;
            student.Photoshop = PhotoshopCheckBox.Checked;
            student.Notes     = NotesTextBox.Text;

            if (!this.IsUpdate)
            {
                using (StudentsManagerContext db = new StudentsManagerContext())
                {
                    db.Students.Add(student);
                    db.SaveChanges();
                }

                MessageBox.Show("Student record is added to the system.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                using (StudentsManagerContext db = new StudentsManagerContext())
                {
                    db.Entry(student).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();

                    MessageBox.Show("Student record is updated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            this.Close();
        }