EntityFramework 学习 一 Explicit Loading with DBContext

即使延迟加载不能使用,也可以通过明确的调用来延迟加载相关实体

使用DBEntryEntity来完成

using (var context = new SchoolDBEntities())
{
    //Disable Lazy loading
    context.Configuration.LazyLoadingEnabled = false;
                
    var student = (from s in context.Students
                        where s.StudentName == "Bill"
                        select s).FirstOrDefault<Student>();

    context.Entry(student).Reference(s => s.Standard).Load();
}     
using (var context = new SchoolDBEntities())
{
    context.Configuration.LazyLoadingEnabled = false;
                
    var student = (from s in context.Students
                        where s.StudentName == "Bill"
                        select s).FirstOrDefault<Student>();

    context.Entry(student).Collection(s => s.Courses).Load();
}
原文地址:https://www.cnblogs.com/lanpingwang/p/6623082.html