【问题标题】:Lazy, Eager and Explicit loading is not working in Entity Framework 5延迟、急切和显式加载在实体框架 5 中不起作用
【发布时间】:2015-12-07 17:56:00
【问题描述】:

部门类:

public partial class Department
{
    public Department()
    {
        this.Courses = new HashSet<Course>();
    }

    public int DepartmentID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Course> Courses { get; set; }

}

课程类别:

public partial class Course
{
     public Course()
     {
         this.Enrollments = new HashSet<Enrollment>();
         this.People = new HashSet<Person>();
     }

     public int CourseID { get; set; }
     public string Title { get; set; }
     public int DepartmentID { get; set; }
     public virtual Department Department { get; set; }

}

上下文:

如下启用延迟加载。

universityDBContext.Configuration.LazyLoadingEnabled = true;
universityDBContext.Configuration.ProxyCreationEnabled = true;

延迟加载在 Entity Framework 5 中不起作用。请帮助

【问题讨论】:

    标签: entity-framework


    【解决方案1】:
    public partial class Department
    {
        public Department()
        {
            this.Courses = new HashSet<Course>();
            this.Students = new HashSet<Student>();
        }
    
        public int DepartmentID { get; set; }
        public string Name { get; set; }
        public decimal Budget { get; set; }
        public System.DateTime StartDate { get; set; }
        public Nullable<int> InstructorID { get; set; }
        public byte[] RowVersion { get; set; }
    
        public virtual ICollection<Course> Courses { get; set; }
        public virtual ICollection<Student> Students { get; set; }
        public virtual Person Person { get; set; }
    }
    

    【讨论】:

      【解决方案2】:
      public partial class Course
      {
          public Course()
          {
              this.Enrollments = new HashSet<Enrollment>();
              this.People = new HashSet<Person>();
          }
      
          public int CourseID { get; set; }
          public string Title { get; set; }
          public int Credits { get; set; }
          public int DepartmentID { get; set; }
          public byte[] rowversion { get; set; }
      
          [ForeignKey("Department")]
          public virtual Department Department { get; set; }
          public virtual ICollection<Enrollment> Enrollments { get; set; }
          public virtual ICollection<Person> People { get; set; }
      }
      

      【讨论】:

      • 我编辑了我的答案,ForeignKey 属性应该在DepartmentID 之上
      • 那么您的模型应该没问题,您可以发布您正在执行的查询吗?
      • masterEntities universityDBContext = new masterEntities(); universityDBContext.Configuration.LazyLoadingEnabled = true; universityDBContext.Configuration.ProxyCreationEnabled = true; var dep = universityDBContext.Departments; foreach (var course in dep) { Console.WriteLine(); }
      • 每个部门可以有多门课程,你怎么可以只在dep中选择一门课程然后尝试将其写入控制台?你事先做了SelectMany 吗?编辑:var result = universityDBContext.Departments.SelectMany(d =&gt; d.Courses).ToList(); foreach(var course in result) Console.WriteLine(course.Title); 应该可以正常工作
      • 此代码 universityDBContext.Departments 选择所有部门以及课程。这不应该是这样的。如果延迟加载正确实现,则只有在课程上完成迭代时才应加载课程
      猜你喜欢
      • 2014-02-18
      • 2011-03-14
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多