实体框架的查找方法无法正常工作 [英] Entity Framework Find method not working properly

查看:79
本文介绍了实体框架的查找方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的课程,称为课程",学生"和老师"

I have classes called Course, Student and Teacher like this

public class Course
    {
        [Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
        public Guid CourseId { set; get; }
        public ICollection<Student> Students { set; get; }
        public Teacher Teacher { get; set; }      

    }



public class Student
    {
        [Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
        public Guid StudentId { set; get; } 
        public ICollection<Course> Courses { set; get; }
    }




public class Teacher
    {
        [Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
        public Guid TeacherId { get; set; }
        public ICollection<Course> Courses { get; set; }
    }

我正在尝试按主键学习课程

I am trying to Get a course by primary key as follow

Course c = _unitOfWork.DbContext.Set<Course>().Find(keyValue);

我从数据库中获取了课程对象,但是课程的学生"和老师"属性为空

i get the course object from the database but the Students and Teacher property of the course are null

我想念东西吗? 谢谢

推荐答案

Find正常工作,因为EF从未加载相关实体.要加载相关属性,您必须使用eager或lazy加载.第三个选项有时称为显式加载.

Find works correctly because EF never loads related entities itself. To load related properties you must either use eager or lazy loading. The third option is sometimes refered as explicit loading.

延迟加载将为您提供相关实体的自动加载,但会生成对数据库的其他查询.首次访问属性时,将加载相关实体或相关集合.要使用延迟加载,必须将实体中的所有导航属性标记为virtual(@ ckal为您提供了示例).同样,只有在用于加载主实体的上下文仍然存在时,延迟加载才起作用.

Lazy loading will provide you automatic loading of related entities but it will generate additional queries to the database. Related entity or related collection will be loaded when you access the property for the first time. To use lazy loading you must mark all navigation properties in the entity as virtual (@ckal provided you an example). Also lazy loading works only if context used to load the main entity is still alive.

急切的加载将定义必须与您的主要实体一起加载的关系.急切的加载是通过Include方法执行的.您可以将Find重写为:

Eager loading will define which relation must be load together with your main entity. Eager loading is executed by Include method. You can rewrite Find as:

Course c = _unitOfWork.DbContext
                      .Set<Course>()
                      .Include(c => c.Students)
                      .Include(c => c.Teacher)
                      .SingleOrDefault(c => c.CourseId == keyValue);

显式加载将允许您明确声明应加载某些关系.您甚至可以定义一些条件来加载相关实体,而这是其他两种方法无法实现的.您必须首先加载主实体,并且在处置上下文之前,您可以执行以下操作:

Explicit loading will allow you to explicitly say that some relation should be loaded. You can even define some condition for loading related entities which is not possible with other two methods. You must first load the main entity and before disposing the context you can do something like:

context.Entry(course).Collection(c => c.Students).Load();

这篇关于实体框架的查找方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆