Advertisement
PeterLindgren

ConsoleApplication2-Program.cs

Mar 4th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 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. using Microsoft.Data.Entity;
  7. using Microsoft.Data.Entity.Infrastructure;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.Logging;
  11.  
  12. using ConsoleApplication2.Model;
  13.  
  14. namespace ConsoleApplication2
  15. {
  16. class Program
  17. {
  18. public static IConfigurationRoot Configuration { get; set; }
  19. public static ApplicationDbContext myDbContext;
  20.  
  21. static void Main(string[] args)
  22. {
  23. Startup(args);
  24. DebugPrint("Start");
  25. Seed(myDbContext);
  26. Test1(myDbContext);
  27. Test2(myDbContext);
  28. DebugPrint("Press Enter to quit:");
  29. Console.ReadLine();
  30. }
  31.  
  32. private static void Test1(ApplicationDbContext myDbContext)
  33. {
  34. var ggparent = (from ggp in myDbContext.GrandGrandParent
  35. .Include(ggp => ggp.GrandParents)
  36. .ThenInclude(x => x.Parents)
  37. where ggp.ID == 3
  38. select ggp).FirstOrDefault();
  39. DebugPrint("GrandGrandParent:");
  40. DebugPrint(ggparent);
  41.  
  42. DebugPrint("WithParents:");
  43. DebugPrint(ggparent);
  44. foreach (var gparent in ggparent.GrandParents)
  45. {
  46. DebugPrint(gparent);
  47. if (gparent.Parents == null)
  48. {
  49. gparent.Parents = (from p in myDbContext.Parent where p.GrandParent.ID == gparent.ID select p).ToList();
  50. }
  51. foreach (var parent in gparent.Parents)
  52. {
  53. DebugPrint(parent);
  54. parent.Children = (from c in myDbContext.Child where c.Parent.ID == parent.ID select c).ToList();
  55. foreach (var child in parent.Children)
  56. {
  57. DebugPrint(child);
  58. }
  59. }
  60. }
  61. int changeCount = myDbContext.SaveChanges();
  62. DebugPrint(string.Format("ChangeCount={0}", changeCount));
  63. }
  64.  
  65.  
  66. private static void Test2(ApplicationDbContext myDbContext)
  67. {
  68. DebugPrint("Second try");
  69.  
  70. var gparents = from gp in myDbContext.GrandParent
  71. .Include(gparent => gparent.GrandGrandParent)
  72. .Include(gparent => gparent.Parents)
  73. .ThenInclude(children => children.Select(child => child.ID))
  74. where gp.GrandGrandParent.ID == 2
  75. select gp;
  76. DebugPrint(gparents.FirstOrDefault().GrandGrandParent);
  77. foreach (var gparent in gparents)
  78. {
  79. DebugPrint(gparent);
  80. if (gparent.Parents == null)
  81. {
  82. gparent.Parents = (from p in myDbContext.Parent where p.GrandParent.ID == gparent.ID select p).ToList();
  83. }
  84. foreach (var parent in gparent.Parents)
  85. {
  86. DebugPrint(parent);
  87. if (parent.Children == null)
  88. {
  89. parent.Children = (from c in myDbContext.Child where c.Parent.ID == parent.ID select c).ToList();
  90. }
  91. foreach (var child in parent.Children)
  92. {
  93. DebugPrint(child);
  94. }
  95. }
  96. }
  97. }
  98.  
  99. private static void Seed(ApplicationDbContext myDbContext)
  100. {
  101. GrandGrandParent grandgrandparent;
  102. GrandParent grandparent;
  103. Parent parent;
  104. Child child;
  105. int g = 1;
  106. if (myDbContext.GrandGrandParent.FirstOrDefault() != null)
  107. {
  108. return;
  109. }
  110. for (int i = 0; i < 4; i++)
  111. {
  112. grandgrandparent = new GrandGrandParent() { Name = FormatName("ggp", i, g++) };
  113. myDbContext.GrandGrandParent.Add(grandgrandparent);
  114. for (int j = 0; j < 4; j++)
  115. {
  116. grandparent = new GrandParent() { Name = FormatName("gp", j, g++), GrandGrandParent = grandgrandparent };
  117. grandgrandparent.GrandParents.Add(grandparent);
  118. myDbContext.GrandParent.Add(grandparent);
  119. for (int k = 0; k < 4; k++)
  120. {
  121. parent = new Parent() { Name = FormatName("p", k, g++), GrandParent = grandparent };
  122. grandparent.Parents.Add(parent);
  123. myDbContext.Parent.Add(parent);
  124. for (int l = 0; l < 4; l++)
  125. {
  126. child = new Child() { Name = FormatName("c", l, g++), Parent = parent };
  127. parent.Children.Add(child);
  128. myDbContext.Child.Add(child);
  129. }
  130. }
  131. }
  132. }
  133. myDbContext.SaveChanges();
  134. }
  135.  
  136. private static string FormatName(string prefix, int local, int global)
  137. {
  138. return String.Format("{0}-{1} ({2})", prefix, local, global);
  139. }
  140.  
  141.  
  142. private static void DebugPrint(GrandGrandParent ggp)
  143. {
  144. DebugPrint(ggp.Name);
  145. }
  146. private static void DebugPrint(GrandParent gp)
  147. {
  148. DebugPrint(" " + gp.Name);
  149. }
  150. private static void DebugPrint(Parent p)
  151. {
  152. DebugPrint(" " + p.Name);
  153. }
  154. private static void DebugPrint(Child c)
  155. {
  156. DebugPrint(" " + c.Name);
  157. }
  158. private static void DebugPrint(string s)
  159. {
  160. //System.Diagnostics.Debug.WriteLine(s);
  161. Console.WriteLine(s);
  162. }
  163.  
  164. public static void Startup(string[] args)
  165. {
  166. var builder = new ConfigurationBuilder();
  167. builder.AddEnvironmentVariables();
  168. builder.AddCommandLine(args);
  169. Configuration = builder.Build();
  170. myDbContext = new ApplicationDbContext();
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement