//0添加失败,-1添加重复 public int addLocation(Location location) { int flag = 0; using (ChooseDishesEntities entities = new ChooseDishesEntities()) { //查询编码是否存在 var type = entities.Location.SingleOrDefault(bt => bt.Code == location.Code && bt.Deleted == 0); if (type == null) { //实体绑定数据 entities.Location.Add(location); try { //操作数据库 flag = entities.SaveChanges(); } catch (Exception ex) { ex.ToString(); } } else { flag = -1; } } return flag; }
//0修改失败,-1修改重复 public int editByLocation(Location local) { int flag = 0; using (ChooseDishesEntities entities = new ChooseDishesEntities()) { //查询编码是否存在 var type = entities.Location.SingleOrDefault(bt => bt.Code == local.Code && bt.Deleted == 0 && bt.LocationId != local.LocationId); if (type == null) { DbEntityEntry<Location> entry = entities.Entry<Location>(local); //设置操作类型 entry.State = System.Data.Entity.EntityState.Unchanged; //设置属性是否参与修改 ,设置为false则无法更新数据 entry.Property("Code").IsModified = true; entry.Property("Name").IsModified = true; entry.Property("UpdateBy").IsModified = true; entry.Property("UpdateDatetime").IsModified = true; try { //关闭实体验证,不关闭验证需要整个对象全部传值 entities.Configuration.ValidateOnSaveEnabled = false; //操作数据库 flag = entities.SaveChanges(); entities.Configuration.ValidateOnSaveEnabled = true; } catch (Exception ex) { ex.ToString(); } } else { flag = -1; } } return flag; }
public int delByLocation(Location location) { int flag = 0; using (ChooseDishesEntities entities = new ChooseDishesEntities()) { DbEntityEntry<Location> entry = entities.Entry<Location>(location); //设置操作类型 entry.State = System.Data.Entity.EntityState.Unchanged; //设置属性是否参与修改 ,设置为false则无法更新数据 entry.Property("Deleted").IsModified = true; entry.Property("UpdateBy").IsModified = true; entry.Property("UpdateDatetime").IsModified = true; try { //关闭实体验证,不关闭验证需要整个对象全部传值 entities.Configuration.ValidateOnSaveEnabled = false; flag = entities.SaveChanges(); entities.Configuration.ValidateOnSaveEnabled = true; } catch (Exception ex) { ex.ToString(); } } return flag; }