How to include derived property of type contained in a List

.net c# entity-framework entity-framework-6 orm

Question

I've got a data context with a list of pipelines. I want to query one pipeline containing all its properties and sub-properties. One pipeline has got a list of stages. The base stage type is abstract. In the list, there can be stages of different child types, here for example it is SpecialStage. A SpecialStage has got a property Settings of type SpecialStageSettings. I want to especially load this SpecialStageSettings object within the query.

With Entity Framework 6.x, I tried

var pipeline = context.Pipelines.Include(x => x.Stages).SingleOrDefault(x => x.Id == idOfAddedPipeline);

but that doesn't work completely, because the Settings property of SpecialStage type stays null. How can I include all sub-properties and sub-sub-properties to be included in the query result?

The data model is the following:

    public abstract class EntityBaseModel<TKey>
    {
        public TKey Id { get; set; }
    }

    public class Pipeline : EntityBaseModel<string>
    {
        public List<Stage> Stages { get; set; }
    }

    public abstract class Stage : EntityBaseModel<string> { }

    public class SpecialStage : Stage
    {
        public SpecialStageSettings Settings { get; set; }
    }

    public abstract class StageSettings : EntityBaseModel<string> { }

    public class SpecialStageSettings : StageSettings
    {
        public double Timeout { get; set; }

        public int TriggerMode { get; set; }
    }
1
0
2/6/2020 2:45:29 PM

Popular Answer

To include Settings entity use Select and specify entity you want to load.

var pipeline = context.Pipelines.Include(x => x.Stages.Select(s => s.Settings)).SingleOrDefault(x => x.Id == idOfAddedPipeline);

You will have to select every entity you wish to load.

0
2/13/2020 6:26:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow