1 EF更新指定欄位.或個更新整個實體 2 3 4 5 更新整個實體: 6 public bool Update(Company compay) 7 { 8 if (compay != null) 9 { 10 dbContext.Entry<Company>(compay).State = EntityState.Modified; 11 } 12 return this.SaveChanges() > 0 ? true : false; 13 } 14 15 16 更新指定欄位: 17 18 public bool Update( Entity.SoldTo soldModel, List<string[]> list) 19 { 20 //更新 21 DbEntityEntry<Entity.SoldTo> uSoldModel = dbContext.Entry<Entity.SoldTo>(soldModel); 22 uSoldModel.State = System.Data.EntityState.Unchanged; 23 24 foreach (string Field in list[0]) 25 { 26 uSoldModel.Property(Field).IsModified = true; 27 } 28 } 29 return this.SaveChanges() > 0 ? true : false; 30 }