public void AddContact(Contact c) { try { DbEntityValidationResult vresult = _enitites.Entry(c).GetValidationResult(); if (vresult.IsValid) { _enitites.Contacts.Add(c); _enitites.SaveChanges(); } else { //Console.WriteLine(vresult.ValidationErrors.First().ErrorMessage); foreach (DbValidationError item in vresult.ValidationErrors) { Console.WriteLine(item.ErrorMessage); } } } catch (DbEntityValidationException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { throw new Exception("Errue générale " + ex.Message); } finally { Trace.Write("tentative d'ajout"); } }
public static void Update2(Contact c) { using (DBContactEntities entities = new DBContactEntities()) { //Classe de performances Stopwatch sw = new Stopwatch(); sw.Start(); //Pour les performances desactiver Le tracking pourpasser en self entities.Configuration.AutoDetectChangesEnabled = false; entities.Contacts.Attach(c); Console.WriteLine(entities.Entry(c).State); c.Adresse = "NEW ADRESS"; Console.WriteLine(entities.Entry(c).State); entities.SaveChanges(); //Affiche les performances sw.Stop(); Console.WriteLine(sw.Elapsed.TotalMilliseconds.ToString()); } }
public static void AddTel(Contact c, string numero) { using (DBContactEntities entities = new DBContactEntities()) { entities.Contact.Attach(c); c.Telephone.Add(new Telephone() { numero = numero }); entities.SaveChanges(); } }
static public void UpdateContact(Contact c) { using (DBContactEntities entities = new DBContactEntities()) { entities.Entry(c).State = System.Data.Entity.EntityState.Modified; Console.WriteLine(entities.Entry(c).State); Console.WriteLine("DataBase Values :"); PrintProp(entities.Entry(c).GetDatabaseValues()); Console.WriteLine("Current Values :"); PrintProp(entities.Entry(c).CurrentValues); entities.SaveChanges(); } }