public static Employee ConvertEmpToEntityFormat(T_Employee t_emp) { try { Employee empRes = new Employee(); empRes.Id = t_emp.Id; empRes.Name = t_emp.Name; empRes.Address = t_emp.Address; empRes.Email = t_emp.Email; empRes.Salary = t_emp.Salary; return(empRes); } catch { return(null); } }
public static T_Employee ConvertEmpToDbFormat(Employee emp) { try { T_Employee empRes = new T_Employee(); empRes.Id = emp.Id; empRes.Name = emp.Name; empRes.Address = emp.Address; empRes.Email = emp.Email; empRes.Salary = emp.Salary; return(empRes); } catch { return(null); } }
//直接编辑,不查询 public void Edit2Employee(Employee emp) { using (dataContext = new EmployeeDataBaseEntities()) { T_Employee empInDb = Common.ConvertEmpToDbFormat(emp); //获取代理对象类的状态为Detaceh System.Data.Entity.Infrastructure.DbEntityEntry entry = dataContext.Entry(empInDb); //1、将代理类的状态修改成 Unchanged 2、将代理类中的需要更新的字段的IsModified修改成true entry.State = System.Data.Entity.EntityState.Unchanged; entry.Property("Name").IsModified = true; entry.Property("Address").IsModified = true; entry.Property("Email").IsModified = true; entry.Property("Salary").IsModified = true; //解决对一个或多个实体验证失败的方法:关闭EF的实体合法性检查 dataContext.Configuration.ValidateOnSaveEnabled = false; //保存 dataContext.SaveChanges(); } }