public IHttpActionResult UpdateCustomer(Customer customer) { try { //Code to Save the data dbContext.Entry(customer).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); //Return Ok response if data saved successfully return(Content(HttpStatusCode.OK, "Data Saved Successfully")); } catch (Exception ex) { return(Content(HttpStatusCode.InternalServerError, "Data Save Failed" + ex.Message)); } }
public static void Test() { using (var dbx = new NorthwindDBContext()) { // Order by ascending var resultat = from obj in dbx.Products orderby obj.UnitPrice ascending select obj; // ----------------------------------------------- // Höj priset med 8% var result = from obj in dbx.Products select obj; double dble = 1.08; decimal d = Convert.ToDecimal(dble); foreach (var Product in dbx.Products) { Product.UnitPrice = Product.UnitPrice * d; Console.WriteLine(Product.UnitPrice); } // ----------------------- // Spara ändringar dbx.SaveChanges(); } }
public bool SaveChanges() { try { return(_db.SaveChanges() >= 0); } catch (Exception ex) { foreach (var entry in _db.ChangeTracker.Entries().Where(e => e.State != EntityState.Unchanged)) { foreach (var prop in entry.CurrentValues.Properties) { var val = prop.PropertyInfo.GetValue(entry.Entity); Console.WriteLine($"{prop.ToString()} ~ ({val?.ToString().Length})({val})"); } } return(_db.SaveChanges() >= 0); } }