迁移到EFCore 2.2后,缺少带有TPH策略的派生类型的ID列

时间:2019-03-12 19:38:42

标签: c# .net entity-framework orm ef-core-2.2

我将应用程序从EF6迁移到EFCore 2.2,并且在将具有派生类型的抽象类型映射到一个表中时遇到了问题。 TPH继承策略。

public abstract class GoalExpectation : Entity { ... }
public class AtLeastGoalExpectation : GoalExpectation { ... }
public class AtMostGoalExpectation : GoalExpectation { ... }

映射和数据库上下文是以这种方式定义的

public class MyDbContext : Microsoft.EntityFrameworkCore.DbContext
{
    public DbSet<GoalExpectation> GoalExpectations { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
         // to avoid declare DbSets for derived types (accoring to docs)
         modelBuilder.Entity<AtLeastGoalExpectation>();
         modelBuilder.Entity<AtMostGoalExpectation>();

         modelBuilder.ApplyConfiguration(new GoalExpectationTypeConfig());
    }
}

public class GoalExpectationTypeConfig : IEntityTypeConfiguration<GoalExpectation>
{
    public void Configure(EntityTypeBuilder<GoalExpectation> builder)
    {
        builder.ToTable("GoalExpectations");

        builder.HasDiscriminator<string>("Discriminator")
            .HasValue<AtLeastGoalExpectation>("AtLeastGoalExpectation")
            .HasValue<AtMostGoalExpectation>("AtMostGoalExpectation")
     }
}

执行此查询时出现以下错误

    public List<Goal> GetGoalsWithTimeLimit(long userId, DateTime date)
    {
        var queryable = _dbContext.Goals
            .Include(p => p.ProgressItems)
            .Where(p => p.Owner.Id == userId)
            .Where(p => p.FinishDate.HasValue || p.ProgressFrequency != null);

        return queryable.ToList();
    }
  

System.Data.SqlClient.SqlException:'无效的列名'AtLeastId'。   无效的列名称'AtMostId'。

0 个答案:

没有答案