简体   繁体   English

实体框架代码First Table-Per-Type继承包括基本类型

[英]Entity Framework Code First Table-Per-Type Inheritance include base type

I have the following model (overly simplified) 我有以下模型(过度简化)

public class Training
{
    public string Name { get; set; }
    public IList<Person> Persons { get; set; }
}
public abstract class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Training Training { get; set; }
}

[Table("Students")]
public class Student : Person
{
    public string StudentNumber { get; set; }
    public IList<Training> Trainings { get; set; }
}
[Table("Instructors")]
public class Instructor : Person
{
    public DateTime StartingDate { get; set; }
    public IList<Training> Trainings { get; set; }
}

I would like to query the database using a training name: 我想使用培训名称查询数据库:

var training = _trainingRepository.FindByName("training class 1", include => include.Persons);

This queries the database and includes all the Student and Instructor. 这将查询数据库并包括所有学生和教师。

Question: 题:

Let's say I have a person that is neither a student or an instructor but he is part of "training class 1". 假设我有一个既不是学生也不是教练的人,但他是“培训班1”的一部分。 I would like to know if it is possible to also get said person in the list of Persons and if so how? 我想知道是否有可能将这个人列入人员名单,如果是这样的话?

Update 更新

The real reason why I am asking the question is because I have 39 distinct derived class and the query built by EF is really slow for obvious reasons. 我问这个问题的真正原因是因为我有39个不同的派生类,由于显而易见的原因,由EF构建的查询真的很慢。 I am trying to get with the initial query only the few most common cases and will get the other cases individually afterward if necessary with a IN (EF Contains) using the base class. 我试图在初始查询中仅使用几个最常见的情况,并且如果需要,可以使用基类IN (EF Contains)中单独获得其他情况。

Answer 回答

The answer was to remove the "abstract" keyword from the Base class (Person). 答案是从Base类(Person)中删除“abstract”关键字。

The answer was to remove the abstract keyword from the Base class (Person): 答案是从Base类(Person)中删除abstract关键字:

FROM: 从:

    public abstract class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Training Training { get; set; }
    }

TO: 至:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Training Training { get; set; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM