Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- using (var context = new MyContext())
- {
- var parentId = Guid.NewGuid();
- context.Parents.Add(new ParentEntity(null)
- {
- Id = parentId,
- Name = "Demo Parent",
- Children = new List<ChildEntity>
- {
- new ChildEntity
- {
- Name = "First Child",
- Id = Guid.NewGuid(),
- ParentId = parentId
- },
- new ChildEntity
- {
- Name = "Second Child",
- Id = Guid.NewGuid(),
- ParentId = parentId
- }
- }
- });
- context.SaveChanges();
- }
- using (var firstContext = new MyContext())
- {
- Console.WriteLine("Call without Include() will LazyLoad");
- var parent = firstContext.Parents.First();
- var firstChild = parent.Children.First();
- Console.WriteLine($"Child: {firstChild.Name}");
- }
- using (var secondContext = new MyContext())
- {
- Console.WriteLine("Call with Include()");
- var parent = secondContext.Parents.Include(x => x.Children).First();
- var secondChild = parent.Children.Last();
- Console.WriteLine($"Child: {secondChild.Name}");
- }
- Console.ReadKey();
- }
- }
Add Comment
Please, Sign In to add comment