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 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 AfficheUnContact2() { Stopwatch sw = new Stopwatch(); sw.Start(); using (DBContactEntities entities = new DBContactEntities()) { entities.Configuration.LazyLoadingEnabled = false; Contact c = entities.Contacts.First(); /*IQueryable<Contact> c = from co in entities.Contacts * select co;*/ string result = c.ToString(); //remonte une collection de navigation (cote many) entities.Entry(c).Collection("Telephones").Load(); foreach (Telephone item in c.Telephones) { Console.WriteLine(item.numero); } //Comptage de donnees en memoires Console.WriteLine(entities.Contacts.Local.Count); sw.Stop(); Console.WriteLine(sw.Elapsed.Milliseconds); } }
public static void ContactParTelephone() { Stopwatch sw = new Stopwatch(); sw.Start(); using (DBContactEntities entities = new DBContactEntities()) { entities.Configuration.LazyLoadingEnabled = false; IQueryable <Telephone> t = entities.Telephones.Where(te => te.idContact == 2); /*IQueryable<Telephone> t = from te in entities.Telephones * where te.idContact == 2 * select te;*/ foreach (Telephone tels in t) { //remonte un element de navigation (cote one) entities.Entry(tels).Reference("Contact").Load(); Console.WriteLine("Id contact : {0}, numero : {1}", tels.Contact.IdContact, tels.numero); } } sw.Stop(); Console.WriteLine(sw.Elapsed.Milliseconds); }
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(); } }