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();
}

 

posted @ 2017-03-26 16:05  蓝平凡  阅读(227)  评论(0编辑  收藏  举报