实体框架6包含()不起作用

时间:2018-04-24 15:18:36

标签: c# entity-framework

我已经在这个问题上看到了很多其他主题,但无法找到解决我确切问题的答案。如果我忽略了它,请道歉。

我有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; }
}

0 个答案:

没有答案