Entity Framework 6 Include () не работает - PullRequest
0 голосов
/ 24 апреля 2018

Я видел много других тем по этой проблеме, но не могу найти ответ, который решает мою точную проблему.Извините, если я пропустил это.

У меня есть 2 типа, Расписание и Работа.Я пытаюсь получить расписание и включить задание, но задание никогда не заполняется.Я добавил показанный оператор отладки для просмотра производимого SQL и он присоединяется к таблице заданий и выбирает все столбцы.Я вставил сгенерированный SQL ниже.

Когда я вручную запускаю SQL для базы данных, он возвращает строки с заполненными столбцами из таблицы Job, т.е. там определенно есть данные.Однако эти данные никогда не используются для создания объекта Job на объектах Schedule.Чего мне не хватает?

    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; }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...