Question

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". 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. 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.

Answer

The answer was to remove the "abstract" keyword from the Base class (Person).

Était-ce utile?

La solution

The answer was to remove the abstract keyword from the Base class (Person):

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; }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top