private static void AlterarProduto() { using (var e = new AdventureWorksEntities()) { var p = e.Produtos.First(); Console.WriteLine(p.Preco); p.Preco = r.Next(1, 101); p.DataDeAlteracao = DateTime.Now; try { e.SaveChanges(); } catch (OptimisticConcurrencyException) { //Console.WriteLine("Client wins"); //e.Refresh(RefreshMode.ClientWins, p); Console.WriteLine("Store wins"); e.Refresh(RefreshMode.StoreWins, p); e.SaveChanges(); } catch (Exception ex) { Console.WriteLine("{0}\n\t{1}", ex.GetType().Name, ex.Message); } finally { Console.WriteLine("{0} -> {1} - {2}", Thread.CurrentThread.ManagedThreadId, p.Nome, p.Preco); } } }
static void Main(string[] args) { using (var e = new AdventureWorksEntities()) { var c = e.Contatos.FirstOrDefault(x => x.ContactID == 19978); c.Nome += "#"; Console.WriteLine(c.Nome); e.SaveChanges(); Console.WriteLine(c.Nome); e.Detach(c); c.Nome += "@"; var key = e.CreateEntityKey("Contatos", c); object o; if (e.TryGetObjectByKey(key, out o)) { e.ApplyCurrentValues(key.EntitySetName, c); e.SaveChanges(); } Console.WriteLine(e.Contatos.FirstOrDefault(x => x.ContactID == 19978).Nome); } Console.ReadKey(); }
static void BwDoWork(object sender, DoWorkEventArgs e) { Console.WriteLine("BwDoWork INI"); using (var awe = new AdventureWorksEntities()) { var c = new Contato() { Nome = "Adão", Sobrenome = "da Silva", PasswordHash = "abc", PasswordSalt = "xyz", rowguid = Guid.NewGuid(), ModifiedDate = DateTime.Now }; awe.Contatos.AddObject(c); awe.SaveChanges(); e.Result = c.ContactID; } Console.WriteLine("BwDoWork FIM"); }
static void Main(string[] args) { using (var e = new AdventureWorksEntities()) { var c = e.Contatos.FirstOrDefault( x => x.Nome.Equals("Adão", /*APAGAR O ADÃO#@*/ StringComparison.InvariantCultureIgnoreCase) && x.Sobrenome.Equals("da Silva", StringComparison.InvariantCultureIgnoreCase)); if (c == null) return; //e.DeleteObject(c); e.Contatos.DeleteObject(c); e.SaveChanges(); Console.WriteLine(e.Contatos.FirstOrDefault( x => x.Nome.Equals("Adão", /*VERIFICAR O ADÃO#@*/StringComparison.InvariantCultureIgnoreCase) && x.Sobrenome.Equals("da Silva", StringComparison.InvariantCultureIgnoreCase)) == null ); } Console.ReadKey(); }
static void Main(string[] args) { using (var e = new AdventureWorksEntities()) { using (var ts = new TransactionScope()) { try { var c = new Contato { Nome = "Adão", Sobrenome = "da Silva", PasswordHash = "abc", PasswordSalt = "xyz", rowguid = Guid.NewGuid(), DataDeAlteracao = DateTime.Now }; e.Contatos.AddObject(c); e.SaveChanges(); Console.WriteLine("inseri o contato {0}", c.ContactID); var f = new Funcionario { ContactID = c.ContactID}; e.Funcionarios.AddObject(f); e.SaveChanges(); ts.Complete(); } catch (Exception ex) { Console.WriteLine("\n{0} -> {1}\n\n\n", ex.GetType().Name, ex.Message); if (ex.InnerException != null) { Console.WriteLine("{0} -> {1}", ex.InnerException.GetType().Name, ex.InnerException.Message); } } } Console.ReadKey(); } }
public void UpdateContact(Contact c) { using (var e = new AdventureWorksEntities()) { e.Attach(e.Contacts.Single(x => x.ContactID == c.ContactID)); e.ApplyCurrentValues("Contacts", c); e.SaveChanges(); } }
public void DeleteContact(Contact c) { if (c == null) return; using (var e = new AdventureWorksEntities()) { e.Attach(c); e.DeleteObject(c); e.SaveChanges(); } }
public int InsertContact(Contact c) { var retorno = 0; using (var e = new AdventureWorksEntities()) { e.AddObject("Contacts", c); e.SaveChanges(); retorno = c.ContactID; } return retorno; }
private static void AlterarContato() { using (var e = new AdventureWorksEntities()) { var c = e.Contatos.First(); Console.WriteLine(c.Nome); c.Nome += ObterComplemento(); c.DataDeAlteracao = DateTime.Now; e.SaveChanges(); Console.WriteLine("{0} -> {1} - {2:dd/MM/yyyy HH:mm:ss.fff}", Thread.CurrentThread.ManagedThreadId, c.Nome, c.DataDeAlteracao); } }
private static void AlterarProduto() { using (var e = new AdventureWorksEntities()) { var p = e.Produtos.First(); Console.WriteLine(p.Preco); p.Preco = ObterPreco(); //p.DataDeAlteracao = DateTime.Now; //ver concurrency mode da prop... e.SaveChanges(); Console.WriteLine("{0} -> {1} - {2}", Thread.CurrentThread.ManagedThreadId, p.Nome, p.Preco); } }
static void Main(string[] args) { using (var e = new AdventureWorksEntities()) { Object o = null; var k = new EntityKey("AdventureWorksEntities.Contatos", "ContactID", 1); if (!e.TryGetObjectByKey(k, out o)) goto fim; var c = o as Contato; if (c == null) goto fim; c.Nome += "*"; Console.WriteLine("{0} {1}", c.Nome, c.Sobrenome); e.SaveChanges(); } fim: //não usar label... isso é só uma brincadeira... Console.ReadKey(); }
static void Main(string[] args) { using (var e = new AdventureWorksEntities()) { //var c = Contato.CreateContato(0, // true, // "Adão", // "da Silva", // 0, // "abc", // "xyz", // Guid.NewGuid(), // DateTime.Now); var c = new Contato { Nome = "Adão", Sobrenome = "da Silva", PasswordHash = "abc", PasswordSalt = "xyz", rowguid = Guid.NewGuid(), ModifiedDate = DateTime.Now }; //e.AddToContatos(c); e.Contatos.AddObject(c); Console.WriteLine("{0} {1}", c.Nome, c.Sobrenome); e.SaveChanges(); Console.WriteLine("ContactID = {0}", c.ContactID); } Console.ReadKey(); }