private void btnDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("are you sure to delete this record ?", "EF Crud Operaton ", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (EFDBEntities db = new EFDBEntities()) { var entry = db.Entry(model); if (entry.State == EntityState.Detached) { db.Customers.Attach(model); } db.Customers.Remove(model); db.SaveChanges(); PopulateDataGridView(); Clear(); MessageBox.Show("Delete successfully"); } } }
private void btnSave_Click(object sender, EventArgs e) { model.FirstName = txtFirstName.Text.Trim(); model.LastName = txtLastName.Text.Trim(); model.City = txtCity.Text.Trim(); model.Address = txtAddress.Text.Trim(); using (EFDBEntities db = new EFDBEntities()) { if (model.CustomerID == 0) // insert { db.Customers.Add(model); } else // update { db.Entry(model).State = EntityState.Modified; } db.SaveChanges(); } Clear(); PopulateDataGridView(); MessageBox.Show("submit successfully"); }