Published on

EntityFramework - Disabled LazyLoading Manually Load Data

關閉 LazyLoading 後,手動載入資料的作法

using (var db = new ContosoUniversityEntities())
{
    db.Configuration.LazyLoadingEnabled = false;

    var c = db.Course.Find(1);

    // 載入單筆資料時 (導覽屬性)
    db.Entry(c).Reference(a => a.Department).Load();

    // 載入多筆資料時 (導覽屬性集合)
    db.Entry(c).Collection(a => a.Person).Load();
}