Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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