I am in the process of trying to convert a mature Entity Framework 6.4.4 project over to Entity Framework Core 8 so (I can be in the future with everyone else) but I am running into the following runtime error:
Here is the what I have for the Model:
public partial class HighlandEntities : DbContext
{
public virtual DbSet<Target> Targets { get; set; }
public virtual DbSet<AccountTarget> AccountTargets { get; set; }
public virtual DbSet<PortfolioTarget> PortfolioTargets { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Target>(entity =>
{
entity.ToTable("Targets").HasDiscriminator<char>("OwnerType")
.HasValue<PortfolioTarget>('P')
.HasValue<AccountTarget>('A');
entity.Property(e => e.TargetID).ValueGeneratedNever();
entity.Property(e => e.RowVersion)
.IsRowVersion()
.IsConcurrencyToken();
});
}
}
public abstract partial class Target : EntityBase
{
[Key]
public Guid TargetID { get; set; }
[Column(TypeName = "date")]
public DateTime? AsOfDate { get; set; }
public double TargetPct { get; set; }
}
public partial class AccountTarget : Target
{
Guid _AccountID;
[Column("OwnerID")]
public virtual Guid AccountID { get; set; }
Guid _InvestmentID;
[Column("BalanceOwnerID")]
public virtual Guid InvestmentID { get; set; }
[ForeignKey(nameof(AccountID))]
[InverseProperty("AccountTargets")]
public virtual Account Account { get; set; }
[ForeignKey(nameof(InvestmentID))]
[InverseProperty("AccountTargets")]
public virtual Investment Investment { get; set; }
}
public partial class PortfolioTarget : Target
{
Guid _PortfolioID;
[Column("OwnerID")]
public virtual Guid PortfolioID { get; set; }
Guid _InvestmentID;
[Column("BalanceOwnerID")]
public virtual Guid InvestmentID { get; set; }
[ForeignKey(nameof(PortfolioID))]
[InverseProperty("PortfolioTargets")]
public virtual Portfolio Portfolio { get; set; }
[ForeignKey(nameof(InvestmentID))]
[InverseProperty("PortfolioTargets")]
public virtual Investment Investment { get; set; }
}
For more context here is the objects in the *.edmx file from the EF 6 version:
How should I change this to get around this error? Thanks.


There are a few issues with your entities.
First, that isn't TPH. TPH has all columns for all sub-classes in a single table using a discriminator. For example you would have a single Target table with a Discriminator (or TargetType etc.) column with values of "Account" vs. "Portfolio". What you have outlined in the schema is actually a TPT model. (Table per Type)
In a TPT if TargetAccount and TargetPortfolio are sub-classes of Target, then each would have a TargetId as their PK with Account-specific details and Portfolio-specific details respectively. When I see things like AccountId and PortfolioId in these tables, with navigation to an Account or Portfolio table then these aren't actually inheritance-based relationships, they are many-to-many joining tables and should be set up simply as a many-to-many joining entity, not an inheritance structure around Target.
I would start by ignoring inheritance all-together at first and look at the data. How do Targets, Portfolios, and Accounts actually relate to one another as the tables are defined? This does look like you might have jumped down an inheritance rabbit hole that is completely unnecessary.