0

I am writing a Program in .Net core (C#), which access a SQLite database using EF core.

namespace BtmManager.Models
{
    public class Projekt
    {
        [Key]
        public int ProjektId { set; get; }
        [Required]
        public int BtmBestandsbuchNr { get; set; }
        [Required]
        public int Stufenanzahl { get; set; }
        public string Produktbezeichnung { get; set; }
        [Required]
        public int ProduktNr { get; set; }
        public DateTime Zeitraum { get; set; }

        public IList<Stufe> Stufen { get; set; }
    }

  public class Stufe
    { 
        [Key]
        public int StufId { set; get; }
        [Required]
        public int StufenNummer { get; set; }
        public string MaterialName { get; set; }
        [Required]
        public int AnzahlEinträge { get; set; }

        public int ProjektId { get; set; }
        public Projekt Projekte { get; set; }

        public IList<Eintrag> Einträge { get; set; }
    }

  public class Eintrag
    {
        [Key]
        public int EintragId { set; get; }
        [Required]
        public byte Einheit { get; set; }
        public string LfdNr { get; set; }
        public DateTime Datum { get; set; }
        public float Anfangsbestand { get; set; }
        public float TheroZugang { get; set; }
        public float PrakZugang { get; set; }
        public float Arbeitsverlust { get; set; }
        public float Abgang { get; set; }
        public float AktuellerBestand { get; set; }
        public string Bemerkung { get; set; }

        public int StufId { get; set; }
        public Stufe Stufen { get; set; }
    }
}

I entered some testvalues
ProjektId BtmBestandsbuchNr Stufenanzahl Produktbezeichnung
1 jnjnijni 2 khih
2 phgztf 1 wdfw
3 yawesxdr 2 ewg

StufId StufenNummer MaterialName AnzahlEinträge ProjektId
1 1 nvjgh 3 1
2 2 jljk 3 1


The hierarchy is:

  • Projekt (Project)
    • Stufen (stage)
      • Eintrag (entry)

My goal is now to show this hierarchy in WPF in a TreeView.

 <TreeView x:Name="TreeView" Margin="5,0,5,0" Grid.Row="1" Grid.RowSpan="7" ItemsSource="{Binding}">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type self:Projekt}" ItemsSource="{Binding Stufen}">
                        <TextBlock Text="{Binding Path=Produktbezeichnung}" />
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type self:Stufe}">
                        <TextBlock Text="{Binding Path=MaterialName}" />
                    </DataTemplate>
                </TreeView.Resources>
            </TreeView>

Result with XAML code

As you can see, the Projects where displayed properly, but no children ! Any idea ??

In case you need to see the whole code, I uploaded it to GitHub

1 Answers1

0

Looking at the source code.

The problem is caused by the default behaviour of entity framework.

Related entities are lazy ( rather than eager ) loaded by default.

If you select an entity, it's related children are not read.

You need to include related children - in this case your Stufen.

There are two ways to do this

How to include() nested child entity in linq

If i follow your entity names correctly:

 .Include("Stufen").ToList();
Andy
  • 11,864
  • 2
  • 17
  • 20