public string GetCategoryEagerLoading()
{
StringBuilder sb = new StringBuilder();
NORTHWNDEntities ctx = new NORTHWNDEntities();
ctx.Configuration.LazyLoadingEnabled = false;
var ca = ctx.Categories.Include(p => p.Products).First(c => c.CategoryID == 1);
foreach (Product p in ca.Products)
{
sb.Append(p.ProductName + ",");
}
return sb.ToString();
}
public string GetCategoryExplicitLoading()
{
StringBuilder sb = new StringBuilder();
NORTHWNDEntities ctx = new NORTHWNDEntities();
ctx.Configuration.LazyLoadingEnabled = false;
var ca = ctx.Categories.First(c => c.CategoryID == 1);
ctx.Entry(ca).Collection(c => c.Products).Load();
foreach (Product p in ca.Products)
{
sb.Append(p.ProductName + ",");
}
return sb.ToString();
}
29 July 2017
Eager loading vs Explicit loading in Entity Framework (using Northwind database)
Microsoft has an excellent article on this
https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment