首页 > 解决方案 > 返回 EF Core (TPH) 中的所有派生模型

问题描述

我有一项任务要求我使用继承 (TPH) 从表中返回所有模型。

我有一个模型类WorkflowInstance和一个派生类CustomWorkflowInstance(它有一个字符串属性)。当然有一个鉴别器。

我想知道一种方法可以在不考虑鉴别器的情况下返回所有元素

public class WorkflowInstance : Entity, ITenantScope, ICorrelationScope
{
    public WorkflowInstance();

    public SimpleStack<ActivityScope> Scopes { get; set; }
    public SimpleStack<ScheduledActivity> ScheduledActivities { get; set; }
    public WorkflowFault? Fault { get; set; }
    public HashSet<BlockingActivity> BlockingActivities { get; set; }
    public IDictionary<string, IDictionary<string, object?>> ActivityData { get; set; }
    public WorkflowOutputReference? Output { get; set; }
    public WorkflowInputReference? Input { get; set; }
    public Variables Variables { get; set; }
    public Instant? FaultedAt { get; set; }
    public Instant? CancelledAt { get; set; }
    public Instant? FinishedAt { get; set; }
    public Instant? LastExecutedAt { get; set; }
    public Instant CreatedAt { get; set; }
    public string? Name { get; set; }
    public string? ContextId { get; set; }
    public string? ContextType { get; set; }
    public string CorrelationId { get; set; }
    public WorkflowStatus WorkflowStatus { get; set; }
    public int Version { get; set; }
    public string? TenantId { get; set; }
    public string DefinitionId { get; set; }
    public ScheduledActivity? CurrentActivity { get; set; }
    public string? LastExecutedActivityId { get; set; }
}

public class CustomWorkflowInstance : WorkflowInstance
{
    public Guid UserId { get; set; }

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<WorkflowInstance>()
             .HasDiscriminator<int>("Discriminator")
             .HasValue(0)
             .HasValue<WorkflowInstance>(0)
             .HasValue<CustomWorkflowInstance>(1);}

在此处输入图像描述

我想找到一种查询表的方法,因为这意味着添加 where 子句 FinishedAt > 等(问题是 UserId 仅存在于派生类中,但所有数据都在鉴别器始终等于 0 的基类中)所以通过选择 *从 WorkflowInstanceTABLE where Used="xx" 它会自动添加 where discriminator = 1 (因为我写了 _dbContext.CustomWorkflowInstance ,其中包含有问题的 userId。

标签: c#.net-coreentity-framework-core

解决方案


推荐阅读