Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication11
  8. {
  9. class Program
  10. {
  11. static void QueryExample()
  12. {
  13. using (var context = new NorthwindEntities())
  14. {
  15. var results = from c in context.Customers
  16. where c.CustomerID.StartsWith("A")
  17. orderby c.CustomerID
  18. select new
  19. {
  20. c.CustomerID,
  21. c.CompanyName,
  22. c.ContactName
  23. };
  24. foreach (var item in results)
  25. {
  26. Console.WriteLine("{0} \t {1} \t {2}",
  27. item.CustomerID,
  28. item.CompanyName,
  29. item.ContactName);
  30. }
  31. }
  32. }
  33. static void InsertExample()
  34. {
  35. using (var context = new NorthwindEntities())
  36. {
  37. var newCategory = new Category()
  38. {
  39. CategoryName = "Fish"
  40. };
  41. context.Categories.Add(newCategory);
  42. context.SaveChanges();
  43. }
  44. Console.WriteLine("Category inserted!");
  45. }
  46. static void UpdateExample()
  47. {
  48. using (var context = new NorthwindEntities())
  49. {
  50. var fishCategory = (from c in context.Categories
  51. where c.CategoryName.Equals("Fish")
  52. select c).First();
  53. fishCategory.CategoryName = "Seafood2";
  54. context.SaveChanges();
  55. }
  56. Console.WriteLine("Category modified!");
  57. }
  58. static void DeleteExample()
  59. {
  60. using (var context = new NorthwindEntities())
  61. {
  62. var seaFoodCategory = (from c in context.Categories
  63. where c.CategoryName.Equals("Seafood2")
  64. select c).First();
  65. context.Categories.Remove(seaFoodCategory);
  66. context.SaveChanges();
  67. }
  68. Console.WriteLine("Category deleted!");
  69. }
  70. static void Main(string[] args)
  71. {
  72. QueryExample();
  73. InsertExample();
  74. UpdateExample();
  75. DeleteExample();
  76. }
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement