public virtual void Delete(TEntity entityToDelete) { if (_context.Entry(entityToDelete).State == EntityState.Detached) { _dbSet.Attach(entityToDelete); } _dbSet.Remove(entityToDelete); }
/// <summary> /// 编辑按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e) { sqlconn = SQLHelper.Connect(); /*try * {*/ //暂时禁用外键关系 SqlCommand sqlCmdNoCheck = new SqlCommand("alter table Department NOCHECK constraint all", sqlconn); SqlCommand sqlCmd2NoCheck = new SqlCommand("alter table Employee NOCHECK constraint all", sqlconn); sqlCmdNoCheck.ExecuteNonQuery(); sqlCmd2NoCheck.ExecuteNonQuery(); //实例化上下文对象 using (EMSEntities context = new EMSEntities()) { //先删除后新增 int DptId = int.Parse(gv1.DataKeys[e.RowIndex].Values[0].ToString()); Department eqp = context.Department.Where <Department>(dpt => dpt.DptId == DptId).FirstOrDefault <Department>(); //告诉上下文要删除 context.Entry <Department>(eqp).State = System.Data.Entity.EntityState.Deleted; //执行删除操作 context.SaveChanges(); Department depart = new Department { DptId = int.Parse((gv1.Rows[e.RowIndex].FindControl("txtId") as TextBox).Text.Trim()), DptName = (gv1.Rows[e.RowIndex].FindControl("txtEqpName") as TextBox).Text.Trim(), DptAdmin = int.Parse((gv1.Rows[e.RowIndex].FindControl("txtDptAdmin") as TextBox).Text.Trim()) }; //告诉上下文要进行添加操作 context.Entry <Department>(depart).State = System.Data.Entity.EntityState.Added; //执行添加操作 context.SaveChanges(); } //恢复外键关系 SqlCommand sqlComdCheck = new SqlCommand("alter table Department CHECK constraint all", sqlconn); sqlComdCheck.ExecuteNonQuery(); SqlCommand sqlComd2Check = new SqlCommand("alter table Employee CHECK constraint all", sqlconn); sqlComd2Check.ExecuteNonQuery(); gv1.EditIndex = -1; PopulateGridView(); lblSuccessMessage.Text = "修改成功!"; lblErrorMessage.Text = ""; /*} * catch (Exception ex) * { * lblSuccessMessage.Text = ""; * lblErrorMessage.Text = ex.Message; * SQLHelper.Close(sqlconn); * } * SQLHelper.Close(sqlconn);*/ }
protected void Button1_Click(object sender, EventArgs e) { sqlconn = SQLHelper.Connect(); try { //暂时禁用外键关系 SqlCommand sqlCmdNoCheck = new SqlCommand("alter table Department NOCHECK constraint all", sqlconn); SqlCommand sqlCmd2NoCheck = new SqlCommand("alter table Employee NOCHECK constraint all", sqlconn); sqlCmdNoCheck.ExecuteNonQuery(); sqlCmd2NoCheck.ExecuteNonQuery(); //实例化上下文对象 using (EMSEntities context = new EMSEntities()) { //先删除后新增 int EmpId = int.Parse(TextBox1.Text.ToString()); Employee emp = context.Employee.Where <Employee>(empl => empl.EmpId == EmpId).FirstOrDefault <Employee>(); //告诉上下文要删除 context.Entry <Employee>(emp).State = System.Data.Entity.EntityState.Deleted; //执行删除操作 context.SaveChanges(); Employee empEdit = new Employee { EmpId = int.Parse(TextBox1.Text.Trim()), EmpName = TextBox2.Text.Trim(), Password = TextBox3.Text.Trim(), EmpPhone = TextBox4.Text.Trim(), DptId = int.Parse(TextBox6.Text.Trim()), IsAdmin = int.Parse(TextBox7.Text.Trim()) }; //告诉上下文要进行添加操作 context.Entry <Employee>(empEdit).State = System.Data.Entity.EntityState.Added; //执行添加操作 context.SaveChanges(); } //恢复外键关系 SqlCommand sqlComdCheck = new SqlCommand("alter table Department CHECK constraint all", sqlconn); SqlCommand sqlComd2Check = new SqlCommand("alter table Employee CHECK constraint all", sqlconn); sqlComdCheck.ExecuteNonQuery(); sqlComd2Check.ExecuteNonQuery(); Label1.Visible = true; Label1.Text = "修改成功!"; } catch (Exception ex) { Label1.Visible = true; Label1.Text = ex.Message; SQLHelper.Close(sqlconn); } SQLHelper.Close(sqlconn); }
public void updateActivity(Activity t) { this.t = t; try { entities.Activities.Attach(t); entities.Entry(t).State = System.Data.Entity.EntityState.Modified; entities.SaveChanges(); } catch (Exception) { entities.Entry(t).State = System.Data.Entity.EntityState.Detached; MessageBox.Show("Something went horribly wrong."); } }
public void updateEvent(Event e) { this.e = e; try { entities.Events.Attach(e); entities.Entry(e).State = System.Data.Entity.EntityState.Modified; entities.SaveChanges(); } catch (Exception) { entities.Entry(e).State = System.Data.Entity.EntityState.Detached; MessageBox.Show("Something went horribly wrong."); } }
public static void EditEmployeeProjectRoleAssignmnet(Project_role Project_role) { EMSEntities datacontext = new EMSEntities(); try { if (Project_role != null) { int empl_status = EmployeeRepo.GetEmployeeStatusById(Project_role.employee_id); ProjectModel proj = ProjectRepo.GetProjectDetailsById(Project_role.project_id); int actual_resource_count = proj.resources_req; List <Project_role_model> assigned_resource = ProjectRepo.GetAssignedProjectRoleList(0, proj.project_id, 0); if ((empl_status == 1) && (proj != null) && (assigned_resource.Count <= actual_resource_count)) { datacontext.Entry(Project_role).State = EntityState.Modified; datacontext.SaveChanges(); } else { throw new Exception("Check Project details , employee status and resource allocation count "); } } } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontext.Dispose(); } }
/// <summary> /// 删除按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e) { sqlconn = SQLHelper.Connect(); try { //暂时禁用外键关系 SqlCommand sqlCmdNoCheck = new SqlCommand("alter table Equipment NOCHECK constraint all", sqlconn); sqlCmdNoCheck.ExecuteNonQuery(); using (EMSEntities context = new EMSEntities()) { //找到要删除的id int EqpId = int.Parse(gv1.DataKeys[e.RowIndex].Values[0].ToString()); Equipment eqp = context.Equipment.Where <Equipment>(equip => equip.EqpId == EqpId).FirstOrDefault <Equipment>(); //告诉上下文要删除 context.Entry <Equipment>(eqp).State = System.Data.Entity.EntityState.Deleted; //执行删除操作 context.SaveChanges(); } //恢复外键关系 SqlCommand sqlComdCheck = new SqlCommand("alter table Equipment CHECK constraint all", sqlconn); sqlComdCheck.ExecuteNonQuery(); PopulateGridView(); lblSuccessMessage.Text = "删除成功!"; lblErrorMessage.Text = ""; } catch (Exception ex) { lblSuccessMessage.Text = ""; lblErrorMessage.Text = ex.Message; SQLHelper.Close(sqlconn); } SQLHelper.Close(sqlconn); }
public static void InactiveEmployee(Employee existinginstance) { EMSEntities datacontext = new EMSEntities(); try { //var query = from employee in datacontext.Employees // join user in datacontext.Users // on employee.user_id equals user.id // where employee.id == e_id && user.is_active ==1 // select user; var query = from user in datacontext.Users where existinginstance.user_id == user.id && user.is_active == 1 select user; User user_details = query.FirstOrDefault(); user_details.is_active = 0; datacontext.Entry(user_details).State = EntityState.Modified; datacontext.SaveChanges(); //datacontext.Entry(query.FirstOrDefault()).State = EntityState.Deleted; //datacontext.SaveChanges(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontext.Dispose(); } }
public IHttpActionResult Put(ClassFeeViewModel mt) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } using (var ctx = new EMSEntities()) { try { var existingClassFee = ctx.CLFEEMSTs.Where(s => s.TRNNO == mt.TRNNO) .FirstOrDefault <CLFEEMST>(); if (existingClassFee != null) { existingClassFee.CLASS_TRNNO = mt.CLASS_TRNNO; foreach (var dtl in mt.CLFEEDTLs) { var mkdetail = new CLFEEDTL { TRNNO = dtl.TRNNO, SR = dtl.SR, FEE = dtl.FEE, ADT = dtl.ADT }; ctx.Entry(mkdetail).State = System.Data.Entity.EntityState.Modified; } ctx.SaveChanges(); } else { return(NotFound()); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } return(Ok()); }
//update public int updateEmpDB(int id, employee employee) { try { int flag = 0; var result = db.employees.Where(e => e.emp_id == id && e.emp_status == 1).FirstOrDefault(); if (result != null) { db.Entry(result).State = EntityState.Detached; db.Entry(employee).State = EntityState.Modified; db.SaveChanges(); flag = 1; } return(flag); } catch (Exception) { throw; } }
public IHttpActionResult DeleteSection(int id) { if (id <= 0) { return(BadRequest("Not a valid student id")); } using (var ctx = new EMSEntities()) { var sec = ctx.SECMSTs .Where(s => s.TRNNO == id) .FirstOrDefault(); var secdtl = ctx.SECDTLs.Where(d => d.TRNNO == id).FirstOrDefault(); ctx.Entry(secdtl).State = System.Data.Entity.EntityState.Deleted; ctx.Entry(sec).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
//update public int updateProgressDB(int id, emp_progresses emp_progresses) { int flag = 0; try { var result = db.emp_progresses.Where(p => p.emp_progress_id == id && p.emp_progress_status == 1).FirstOrDefault(); if (result != null) { db.Entry(result).State = EntityState.Detached; db.Entry(emp_progresses).State = EntityState.Modified; db.SaveChanges(); flag = 1; } } catch (Exception) { throw; } return(flag); }
//update method public int updateClientDB(int id, client client) { int flag = 0; try { var result = db.clients.Where(c => c.client_id == id).FirstOrDefault(); if (result != null) { db.Entry(result).State = EntityState.Detached; db.Entry(client).State = EntityState.Modified; db.SaveChanges(); flag = 1; } } catch (Exception) { throw; } return(flag); }
//update method public int updateTaskDB(int id, task task) { int flag = 0; try { var result = db.tasks.Where(t => t.task_id == id && t.task_status == 1).FirstOrDefault(); if (result != null) { db.Entry(result).State = EntityState.Detached; db.Entry(task).State = EntityState.Modified; db.SaveChanges(); flag = 1; } } catch (Exception) { throw; } return(flag); }
//update method public int updateProjectProgressDB(int id, project_progress progress) { int flag = 0; try { var result = db.project_progress.Where(p => p.progress_id == id).FirstOrDefault(); if (result != null) { db.Entry(result).State = EntityState.Detached; db.Entry(progress).State = EntityState.Modified; db.SaveChanges(); flag = 1; } } catch (Exception) { throw; } return(flag); }
public IHttpActionResult DeleteFeeCollect(int id) { if (id <= 0) { return(BadRequest("Not a valid student id")); } using (var ctx = new EMSEntities()) { var fc = ctx.FEECOLLECTMSTs .Where(s => s.TRNNO == id) .FirstOrDefault(); ctx.Entry(fc).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
public IHttpActionResult DeleteEdu(int id) { if (id <= 0) { return(BadRequest("Not a valid student id")); } using (var ctx = new EMSEntities()) { var edu = ctx.EDUCATIONs .Where(s => s.TRNNO == id) .FirstOrDefault(); ctx.Entry(edu).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
public IHttpActionResult DeleteLeaveType(int id) { if (id <= 0) { return(BadRequest("Not a valid student id")); } using (var ctx = new EMSEntities()) { var ltp = ctx.LEAVETYPEs .Where(s => s.TRNNO == id) .FirstOrDefault(); ctx.Entry(ltp).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
public IHttpActionResult DeleteMarkTotal(int id) { if (id <= 0) { return(BadRequest("Not a valid student id")); } using (var ctx = new EMSEntities()) { var mt = ctx.MARKTOTALs .Where(s => s.TRNNO == id) .FirstOrDefault(); ctx.Entry(mt).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
public IHttpActionResult DeleteDept(int id) { if (id <= 0) { return(BadRequest("Not a valid Department id")); } using (var ctx = new EMSEntities()) { var dept = ctx.DEPTs .Where(s => s.TRNNO == id) .FirstOrDefault(); ctx.Entry(dept).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
public IHttpActionResult DeleteComp(int id) { if (id <= 0) { return(BadRequest("Not a valid Company id")); } using (var ctx = new EMSEntities()) { var bp = ctx.COMPANies .Where(s => s.TRNNO == id) .FirstOrDefault(); ctx.Entry(bp).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
public static void UpdateUserRole(User_role userrole) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(userrole).State = EntityState.Modified; datacontext.SaveChanges(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontext.Dispose(); } }
public static void UpdateSalaryStructure(Salary_Structure salary) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(salary).State = EntityState.Modified; datacontext.SaveChanges(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontext.Dispose(); } }
public static void UpdateTaxDeclaration(Incometax incometax) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(incometax).State = EntityState.Modified; datacontext.SaveChanges(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontext.Dispose(); } }
public static void DeleteTaskById(Task instance) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(instance).State = EntityState.Deleted; datacontext.SaveChanges(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontext.Dispose(); } }
public static void UpdateLeaveBalanceSheet(Leavebalance_sheet leave_balance) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(leave_balance).State = EntityState.Modified; datacontext.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.GetBaseException()); throw e; } finally { datacontext.Dispose(); } }
public static void EditUserPassword(User user) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(user).State = EntityState.Modified; datacontext.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.GetBaseException()); throw e; } finally { datacontext.Dispose(); } }
public static void DeleteHolidayList(Holiday_List holiday_list) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(holiday_list).State = EntityState.Deleted; datacontext.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.GetBaseException()); throw e; } finally { datacontext.Dispose(); } }
public static void EditClient(Client client) { EMSEntities datacontext = new EMSEntities(); try { datacontext.Entry(client).State = EntityState.Modified; datacontext.SaveChanges(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontext.Dispose(); } }
public static void EditEmployee(Employee Details) { EMSEntities datacontent = new EMSEntities(); try { datacontent.Entry(Details).State = EntityState.Modified; datacontent.SaveChanges(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.GetBaseException()); throw exception; } finally { datacontent.Dispose(); } }