Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在实体框架中包含来自派生对象的集合_C#_Entity Framework - Fatal编程技术网

C# 如何在实体框架中包含来自派生对象的集合

C# 如何在实体框架中包含来自派生对象的集合,c#,entity-framework,C#,Entity Framework,对不起,那个标题不太好。假设我有一些课程: public abstract class Person { public int ID { get; set; } } public class Student : Person { public Student() { Grades = new List<Grades>(); } public virtual ICollection<Grades> Grades { get;

对不起,那个标题不太好。假设我有一些课程:

public abstract class Person {
    public int ID { get; set; }
}

public class Student : Person {
    public Student() {
        Grades = new List<Grades>();
    }
    public virtual ICollection<Grades> Grades { get; set; }
}

public class Teacher : Person {
    public string Class { get; set; }
}
public class Grades {
    public int Score { get; set; }
}
但我该怎么做:

db.People.where(p => p is Student).Include(s => s.Grades)

不打开lazyloading?

您只需将Grades属性标记为非虚拟(删除虚拟关键字)即可为此禁用延迟加载。

您可以使用OfType按类型筛选,然后对其应用包含

var students = db.People.OfType<Student>().Include(s => s.Grades);
var students=db.People.OfType()。包括(s=>s.Grades);
var students = db.People.OfType<Student>().Include(s => s.Grades);