1

我在这个问题上看到了很多其他线程,但找不到解决我确切问题的答案。抱歉,如果我忽略了它。

我有 2 种类型,Schedule 和 Job。我正在尝试检索计划并包含作业,但从未填充作业。我添加了显示的调试语句来查看正在生成的 SQL,它确实加入了 Job 表并选择了所有列。我在下面粘贴了生成的 SQL。

当我对数据库手动运行 SQL 时,它会返回填充了 Job 表中的列的行,即那里肯定有数据。但是,该数据永远不会用于在 Schedule 对象上创建 Job 对象。我错过了什么?

    public List<Schedule> GetActiveSchedules()
    {
        using (var context = new SchedulerContainer())
        {
            context.Database.Log = s => _logger.Debug(s);

            return context.Schedules
                .Where(s => s.IsEnabled)
                .Include(s => s.Job)
                .ToList();
        }
    }

SELECT 
[Extent1].[ScheduleId] AS [ScheduleId], 
[Extent1].[JobId] AS [JobId], 
[Extent1].[IsEnabled] AS [IsEnabled], 
[Extent1].[ValidFrom] AS [ValidFrom], 
[Extent1].[ValidTo] AS [ValidTo], 
[Extent1].[ScheduleTypeId] AS [ScheduleTypeId], 
[Extent1].[Schedule] AS [Schedule], 
[Extent1].[MaxDelay] AS [MaxDelay], 
[Extent1].[MaxRetries] AS [MaxRetries], 
[Extent1].[RetryInterval] AS [RetryInterval], 
[Extent1].[Description] AS [Description], 
[Extent1].[NonWorkingDayStrategyId] AS [NonWorkingDayStrategyId], 
[Extent1].[Param] AS [Param], 
[Extent2].[JobId] AS [JobId1], 
[Extent2].[IJobImplementation] AS [IJobImplementation], 
[Extent2].[IsEnabled] AS [IsEnabled1], 
[Extent2].[Description] AS [Description1]
FROM  [scheduler].[Schedule] AS [Extent1]
INNER JOIN [scheduler].[Job] AS [Extent2] ON [Extent1].[JobId] = [Extent2].[JobId]
WHERE [Extent1].[IsEnabled] = 1

以下是其中一位评论者要求的 Schedule 和 Job 类的外观。

public partial class Schedule
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Schedule()
    {
        this.ScheduledJobs = new HashSet<ScheduledJob>();
        this.Dependencies = new HashSet<Dependency>();
        this.Groups = new HashSet<Group>();
    }

    public int ScheduleId { get; set; }
    public int JobId { get; set; }
    public bool IsEnabled { get; set; }
    public Nullable<System.DateTime> ValidFrom { get; set; }
    public Nullable<System.DateTime> ValidTo { get; set; }
    public ScheduleTypeEnum ScheduleTypeId { get; set; }
    public string Schedule1 { get; set; }
    public Nullable<int> MaxDelay { get; set; }
    public int MaxRetries { get; set; }
    public Nullable<int> RetryInterval { get; set; }
    public string Description { get; set; }
    public int NonWorkingDayStrategyId { get; set; }
    public string Param { get; set; }

    public virtual Job Job { get; set; }
    public virtual ScheduleType ScheduleType { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ScheduledJob> ScheduledJobs { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Dependency> Dependencies { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Group> Groups { get; set; }
    public virtual NonWorkingDayStrategy NonWorkingDayStrategy { get; set; }
}

public partial class Job
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Job()
    {
        this.Schedules = new HashSet<Schedule>();
        this.ScheduledJobs = new HashSet<ScheduledJob>();
        this.Dependencies = new HashSet<Dependency>();
    }

    public int JobId { get; set; }
    public string IJobImplementation { get; set; }
    public bool IsEnabled { get; set; }
    public string Description { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Schedule> Schedules { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ScheduledJob> ScheduledJobs { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Dependency> Dependencies { get; set; }
}
4

0 回答 0