c# - 返回 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。
解决方案
推荐阅读
- android - Android,即使授予权限,也拒绝创建文件的权限
- listview - 如何访问列表视图中的单个项目以更新进度条指示器
- sql - 如何使用 MDX SQL 选择具有相同产品名称的多行的最大值?
- node.js - 如何从 Cloudfront CDN 提供视图
- django - Django:到默认数据库的 2 个连接?
- python - 没有名为 bokeh 的模块
- html - 如何将大链接列表显示为 4 列但在表格中?
- sql - SQL Server 可以同时处理多个查询吗?
- build - 尝试在 Azure DevOps Pipeline 中构建时未找到项目文件
- sql - 在 SQL 中创建数据立方体