Пример #1
0
 /// <summary>
 /// 添加一个学校
 /// </summary>
 static void AddSchool()
 {
     using (EFDomeEntities context = new EFDomeEntities())
     {
         context.Database.Log = Console.WriteLine;
         context.School.Add(
             new School {
             name = "nf149"
         }
             );
         context.SaveChanges();
     }
 }
Пример #2
0
        /// <summary>
        /// 关闭延迟加载,使用显示加载
        /// </summary>
        static void ExplicitLoading()
        {
            using (EFDomeEntities context = new EFDomeEntities())
            {
                context.Database.Log = Console.WriteLine;
                //设置关闭延迟加载
                context.Configuration.LazyLoadingEnabled = false;

                // 我们去数据库中找Student,里面并没有School对象的信息,也就是School为null
                var item = context.Student.FirstOrDefault();

                //我们手动去将School 这个对象进行显示加载
                context.Entry(item).Reference(s => s.School).Load();
                //如果是集合可以使用
                //context.Entry(item).Collection(s => s.School).Load();

                var school = item.School;
            }
        }