Last active
May 16, 2018 00:13
-
-
Save dcomartin/6510c8f3b89b0d6127be649a87d581d3 to your computer and use it in GitHub Desktop.
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