This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment