Context - using ef core 7 w/ reverse engineering (database first)
Desired Outcome:
- there is a calculated column in a table that I only want to fetch in certain situations
- by default, I don't want it to be fetched with the base entity
- ideally, not have to use custom projections everywhere I don't want the value
I was hoping I could do something like have a class that inherits the entity for this table and the new class has the calculated column (don't include it in the reverse engineering and add it manually). That way when I fetch the base entity it isn't included but if I fetch the new class it does.
I tried to do some table splitting but the issue there is there isn't really a Discriminator for these types. Each row can represent either type, so you can't really discriminate by some arbitrary column value. But I end up with the error SqlException: Invalid column name 'Discriminator'. Statement(s) could not be prepared. without a discriminator.
At a high level, this is the code being used.
//Entities
public class A {
public long Id { get; set; }
public string Name { get; set; }
}
public class B: A {
public long CalculatedValue { get; set; }
}
//dbcontext extension
public virtual DbSet<B> B { get; set; }
partial void OnModelCreatingPartial(ModelBuilder modelBuilder) {
modelBuilder.Entity<B>(entity =>
{
entity.hasBaseType<A>();
});
}