Guest User

Untitled

a guest
May 21st, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. using (var context = new MyContext())
  6. {
  7. var parentId = Guid.NewGuid();
  8. context.Parents.Add(new ParentEntity(null)
  9. {
  10. Id = parentId,
  11. Name = "Demo Parent",
  12. Children = new List<ChildEntity>
  13. {
  14. new ChildEntity
  15. {
  16. Name = "First Child",
  17. Id = Guid.NewGuid(),
  18. ParentId = parentId
  19. },
  20. new ChildEntity
  21. {
  22. Name = "Second Child",
  23. Id = Guid.NewGuid(),
  24. ParentId = parentId
  25. }
  26. }
  27. });
  28. context.SaveChanges();
  29. }
  30.  
  31. using (var firstContext = new MyContext())
  32. {
  33. Console.WriteLine("Call without Include() will LazyLoad");
  34. var parent = firstContext.Parents.First();
  35. var firstChild = parent.Children.First();
  36. Console.WriteLine($"Child: {firstChild.Name}");
  37. }
  38.  
  39. using (var secondContext = new MyContext())
  40. {
  41. Console.WriteLine("Call with Include()");
  42. var parent = secondContext.Parents.Include(x => x.Children).First();
  43. var secondChild = parent.Children.Last();
  44. Console.WriteLine($"Child: {secondChild.Name}");
  45. }
  46.  
  47. Console.ReadKey();
  48. }
  49. }
Add Comment
Please, Sign In to add comment