Posted By

mroma on 06/27/12


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

danfinrud55


EF Code


 / Published in: C#
 

EF Code

  1. private void list_Click(object sender, EventArgs e)
  2. {
  3. using (var db = new Db())
  4. {
  5. var cs = db.Customers.ToList();
  6.  
  7. foreach (Customer c in cs)
  8. {
  9. Console.WriteLine(c.Name);
  10. }
  11. }
  12.  
  13. Console.WriteLine("listed! " + DateTime.Now.ToString());
  14. }
  15.  
  16. private void add_Click(object sender, EventArgs e)
  17. {
  18. using (var db = new Db())
  19. {
  20. var c = new Customer { Name = "Hello2"};
  21. db.Customers.MyInsertOrUpdate(c, db);
  22. db.SaveChanges();
  23. }
  24.  
  25. Console.WriteLine("added! " + DateTime.Now.ToString());
  26. }
  27.  
  28. private void update_Click(object sender, EventArgs e)
  29. {
  30. using (var db = new Db())
  31. {
  32. var c = new Customer { id = 2, Name = "New1", Type = 2 };
  33. db.Customers.MyInsertOrUpdate(c, db);
  34. db.SaveChanges();
  35. }
  36.  
  37. Console.WriteLine("updated! " + DateTime.Now.ToString());
  38. }
  39.  
  40. private void delete_Click(object sender, EventArgs e)
  41. {
  42. using (var db = new Db())
  43. {
  44. var c = new Customer { id = 3 };
  45. db.Customers.Attach(c);
  46. db.Customers.Remove(c);
  47. db.SaveChanges();
  48. }
  49.  
  50. Console.WriteLine("deleted! " + DateTime.Now.ToString());
  51. }
  52.  
  53. private void deleteMany_Click(object sender, EventArgs e)
  54. {
  55. using (var db = new Db())
  56. {
  57.  
  58. db.Customers
  59. .Where(x => x.Name.StartsWith("Hello"))
  60. .ToList()
  61. .ForEach(i => db.Customers.Remove(i));
  62. db.SaveChanges();
  63. }
  64.  
  65. Console.WriteLine("deleted many! " + DateTime.Now.ToString());
  66. }

Report this snippet  

You need to login to post a comment.