Reputation: 1299
I'm trying to use inheritance in EF and have come up with the following (simplified):
public abstract class Job
{
public int Id { get; set; }
public DateTime ScheduledTime { get; set; }
}
[Table("InstallationJobs")]
public class InstallationJob : Job
{
public PbnSite Site { get; set; }
}
And my dbcontext has the following:
public DbSet<Job> Jobs { get; set; }
Now, I'm trying to get a random job (which I've previously inserted):
var job = _dbContext.Jobs.OrderBy(x => x.ScheduledTime).FirstOrDefault();
//logic to find out which job time was retrieved and then get the specific information
And that returns a job! However, when I cast the job to the correct job type (InstallationJob in this example), it doesn't have any information in regards to the Site property. (since I haven't included it).
But how would I go about including it seeing as Jobs is a generic table?
When the job is of childtype X I want to include the properties of X and when it is of Y I want to include the properties of Y.
Upvotes: 1
Views: 139
Reputation: 18181
You can get the derived object using OfType
method
var job = _dbContext.Jobs.OrderBy(x => x.ScheduledTime)
.OfType<InstallationJob >
.FirstOrDefault();
Upvotes: 1