Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active May 16, 2018 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/6510c8f3b89b0d6127be649a87d581d3 to your computer and use it in GitHub Desktop.
Save dcomartin/6510c8f3b89b0d6127be649a87d581d3 to your computer and use it in GitHub Desktop.
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