0

I am new to EF Core and am trying to use TPH Inheritance with Entity Framework Core

I have the following classes defined

    public class WorkItem {
        public Guid Id { get; set; }
        public string WorkItemType { get; set; }
        public string Description { get; set; }
    }
    public class Job : WorkItem {
        public string BillingNotes { get; set; }
    }

In my context, I have

    public class JobContextNew : DbContext {
        public virtual DbSet<WorkItem> WorkItem { get; set; }
        public virtual DbSet<Job> Job { get; set; }

        public JobContextNew(DbContextOptions<JobContextNew> options) : base(options) { }

        protected override void OnModelCreating(ModelBuilder modelBuilder) {
            modelBuilder.Entity<WorkItem>(entity => entity.Property(e => e.Id).ValueGeneratedNever());
            modelBuilder.Entity<WorkItem>()
                .HasDiscriminator(workitem => workitem.WorkItemType)
                .HasValue<Job>(nameof(Job));
        }
    }

If I omit the field in Job, it will pull the data just fine but when I add the BillngNotes back in I get the following error: Invalid column name 'BillingNotes

Can anyone tell me what I might be doing wrong?

Chris
  • 1
  • 1
  • I have resorted to using TPT which appears to be working fine but an answer as to why TPH would not work would be greatly appreciated – Chris Jul 20 '21 at 16:42
  • Can you please show the structure of your database? Did you create one whole workitem table for the TPH approach? – troYman Oct 29 '21 at 08:32

0 Answers0