Why isn't the TreeView showing the Children - wpf

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>
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

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();

Related

Entities with problems and limitations

I have these entities:
public class Project : Base
{
public string Name { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public ProjectStatus CurrentStatus { get; set; } = ProjectStatus.New;
public List<StatusHistory>? StatusHistories { get; set; }
public Team? Team { get; set; }
public int? TeamId { get; set; }
// public ProgressTrackerValue trackerValue { get; set; }
public List<AttachedFile> AttachedFiles { get; set; }
}
public class Task : Base
{
public string Name { get; set; }
public string Description { get; set; }
public DateTime? EstimatedDueDate { get; set; }
public DateTime? ActualDueDate { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; } // ?? End DATE same as ActualDueDate
public TaskStatus CurrentStatus { get; set; } = TaskStatus.Draft;
public int ProjectId { get; set; }
public Project Project { get; set; }
public List<StatusHistory> StatusHistories { get; set; }
public TaskType TaskType { get; set; }
public TaskPriority Priority { get; set; }
public float ? DurationProgress { get; set; }
public List<AttachedFile> AttachedFiles { get; set; }
}
public class Team : Base
{
public string TeamLeaderId { get; set; }
public List<TeamMember>? TeamMembers { get; set; }
public virtual Project Project { get; set; }
}
public class AttachedFile : Base
{
public string OriginalFileName { get; set; }
public string FileName { get; set; }
public string MimeType { get; set; }
public string FileType { get; set; }
public string OwnerType { get; set; }
public int OwnerId {get; set;}
public string FilePath { get; set; }
public long FileSize { get; set; }
}
public class Comment : Base
{
public string Content { get; set; }
public int? ParentCommentId { get; set; }
public List<Comment>? Replies { get; private set; }
public Task Task { get; set; }
public int taskId { get; set; }
public List<AttachedFile>? AttachedFiles { get; set; }
}
public class StatusHistory : Base
{
public string OwnerType { get; set; }
public int OwnerId {get; set;}
public string State { get; set; }
}
public class TaskEmployee : Base
{
public int TaskId { get; set; }
public Task Task { get; set; }
public string EmployeeId { get; set; }
}
public class TeamMember : Base
{
public string UserId { get; set; }
}
What can I do to improve the above entities? Any suggestions.
Also there is UserId,EmployeeId,TeamLeaderId.... all of these supposed to be FK to IdentityUser but this project is isolated from Identity and i can't find a way to link them together. if there is a suggestion on how could I make such relationship in EFCore. (I could make relationship manually but I can't put navigation properties for Identity inside these entities thus not benefiting from EFCore).
Finally there is OwnerType, OwnerId, for example in the AttachedFile entity the attached file could be related to Project, Task, Comment. So I will store the type and id for them in these fields, the problem however that I will not benefit form EF Core functionality this way. For example: I must write SQL manually for every query.
currently the schema look like this:
enter image description here

Error in creating SQL Database from EF model

I have a problem when I want to create my SQL Server database from my EF model.
Here's my model :
public class BilanPatrimonial
{
public int ID { get; set; }
[Key, ForeignKey("EtatCivil")]
public int EtatCivilMonsieurID { get; set; }
public EtatCivil EtatCivilMonsieur { get; set; }
public int EtatCivilMadameID { get; set; }
public EtatCivil EtatCivilMadame { get; set; }
}
public class EtatCivil
{
public int ID { get; set; }
public string Prenoms { get; set; }
public DateTime DateNaissance { get; set; }
public string LieuNaissance { get; set; }
public string Nationalite { get; set; }
public StatutProfessionnel StatutProfessionnel { get; set; }
public string Profession { get; set; }
public string NomEmployeur { get; set; }
public string Activité { get; set; }
public int Ancienneté { get; set; }
public int SalaireAnnuelNet { get; set; }
public int Rentes { get; set; }
public int PensionsNettes { get; set; }
public int BNCBICAnnuellesNetsNMoins1 { get; set; }
public int BNCBICAnnuellesNetsNMoins2 { get; set; }
public bool CentreGestionAgree { get; set; }
public int BeneficeDeficitFoncier { get; set; }
public int DividendesNMoins1 { get; set; }
public int DividendesNMoins2 { get; set; }
}
I have 2 objects of type EtatCivil
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Annonce>().ToTable("Annonces");
modelBuilder.Entity<HonorairesAChargeDe>().ToTable("HonorairesAChargeDe");
modelBuilder.Entity<TypeChauffage>().ToTable("TypesChauffage");
modelBuilder.Entity<TypeCuisine>().ToTable("TypesCuisine");
modelBuilder.Entity<Favori>().ToTable("Favoris");
modelBuilder.Entity<Agenda>().ToTable("Agendas");
modelBuilder.Entity<Evenement>().ToTable("Evenements");
modelBuilder.Entity<Offre>().ToTable("Offres");
modelBuilder.Entity<BilanPatrimonial>().ToTable("BilansPatrimoniaux");
modelBuilder.Entity<EtatCivil>().ToTable("EtatCivils");
//modelBuilder.Entity<ChargesImmobilier>().ToTable("ChargesImmobiliers");
//modelBuilder.Entity<AutresChargesMensuelles>().ToTable("AutresChargesMensuelless");
//modelBuilder.Entity<Placement>().ToTable("Placements");
}
When I try to create the database, the FOREIGN KEY 'FK_BilansPatrimoniaux_EtatCivils_EtatCivilMonsieurID1' cannot be created because of cascading cycles or access.
The message tells me to specify ON DELETE NO ACTION or ON UPDATE NO ACTION but I don't know how to do this.
Any help would be really appreciated, thanks

how to combine multiple models into one viewmodel based on a condition ? (WPF)

I have the following three models:
namespace Company.Model {
class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
} }
and
namespace Company.Model
{
class Employee
{
public int EmployeeId { get; set; }
public string EmployeeFirstName { get; set; }
public string EmployeeMiddleName { get; set; }
public string EmployeeLastName { get; set; }
public DateTime? EmployeeDOB { get; set; }
public char? EmployeeGender { get; set; }
public string EmployeeAddress { get; set; }
public int? EmployeeContactNumber { get; set; }
public string EmployeeEmailID { get; set; }
public DateTime? EmployeeJoiningDate { get; set; }
public DateTime? EmployeeConfirmationDate { get; set; }
public DateTime? EmployeeExpectedInTime { get; set; }
public DateTime? EmployeeExpectedOutTime { get; set; }
public bool? EmployeeIsResigned { get; set; }
public double? EmployeeSalary { get; set; }
public string EmployeeDesignation { get; set; }
public int? DepartmentId { get; set; }
public int? CountryId { get; set; }
}
}
and
namespace Company.Model
{
class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
}
Now I need to combine all three into one viewmodel, based on the condition that Employee.CountryId=Country.CountrId and Employee.DepartmentId=Department.DepartmentId.
I basically want a join on the tables, so as to get country name and department name of each employee.

Invalid object name 'dbo.customers1'

I'm getting the error "Invalid object name 'dbo.customers1'" on my view...
#foreach (var item in Model)
{
<tr>
<td>#item.orderid</td>
<td>#item.customer.firstname</td>
I have the ViewModel classes...
public class orders
{
[Key]
public int orderid { get; set; }
public System.DateTime createdate { get; set; }
public string createdby { get; set; }
public Nullable<int> statusid { get; set; }
public Nullable<System.DateTime> pickup { get; set; }
public Nullable<System.DateTime> dropoff { get; set; }
public Nullable<System.DateTime> scheduledout { get; set; }
public Nullable<System.DateTime> scheduledin { get; set; }
public bool instorepickup { get; set; }
public string paymenttype { get; set; }
public System.DateTime reservationstart { get; set; }
public System.DateTime reservationend { get; set; }
public bool morningpickup { get; set; }
public Nullable<int> customerid { get; set; }
public string notes { get; set; }
public string shippingtype { get; set; }
public Nullable<decimal> shippingestimate { get; set; }
public virtual customer customer { get; set; }
public virtual ICollection<invoice> invoices { get; set; }
public virtual orderstatuses orderstatuses { get; set; }
public virtual ICollection<serialorders> serialorders { get; set; }
}
and
public class customers
{
[Key]
public int customerid { get; set; }
public System.DateTime createdate { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
[Required]
public string billingaddress1 { get; set; }
public string billingaddress2 { get; set; }
[Required]
public string billingcity { get; set; }
public string billingstate { get; set; }
[Required]
public string billingzip { get; set; }
[Required]
public string billingcountry { get; set; }
public string phone { get; set; }
[Required]
public string email { get; set; }
[Required]
public string shippingaddress1 { get; set; }
public string shippingaddress2 { get; set; }
[Required]
public string shippingcity { get; set; }
public string shippingstate { get; set; }
[Required]
public string shippingzip { get; set; }
[Required]
public string shippingcountry { get; set; }
public bool goodstanding { get; set; }
public string userid { get; set; }
public Nullable<DateTime> insuranceexp { get; set; }
public virtual ICollection<invoice> invoices { get; set; }
public virtual ICollection<order> orders { get; set; }
}
this is my model...
And I have a data access layer...
public DbSet<tvc.viewModels.orders> orders { get; set; }
public DbSet<tvc.viewModels.customers> customers { get; set; }
The error is pretty straight-forward. There's no dbo.customers1 table in your database. As to why, there's really not enough information here to say, but the most likely causes are:
You're using Database First or Code First with an existing database, and your entities are out of sync with the database.
You're using Code First, don't have migrations enabled, and you've made a change to either your entities or your database. Again, out of sync.
You've specified an explicit table name, either via the Table attribute or fluent config, that doesn't exist. Either way, change either the explicit table name to what it should be or rename your database table so it matches.
There was an oversight in my ViewModel. I was pointing customer, which is the name of the database table and the model class, rather than the viewmodel class which is customers (with an s).

How to generate a unique GUID when creating a new object in Entity Framework 5?

I've similar questions that refer to this annotation to solve their problem:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
However, applying this to my relevant Entity Model does nothing.
When I call the .Add() method, it inserts the GUID as 00000000-0000-0000-0000-000000000000.
As you can see, the annotation I mentioned is present in the model:
using System;
using System.Collections.Generic;
public partial class Event
{
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public System.Guid ID { get; set; }
public int AccountID { get; set; }
public string ExternalID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Location { get; set; }
public Nullable<System.DateTime> StartTime { get; set; }
public Nullable<System.DateTime> EndTime { get; set; }
public Nullable<bool> AllDay { get; set; }
public Nullable<System.Guid> ImageData { get; set; }
public string ImageUrl { get; set; }
public virtual Account Account { get; set; }
public virtual FetchedFile FetchedFile { get; set; }
}
How can I generate a unique GUID upon inserting a new Entity Object?
You can always initialize the Guid in a constructor.
public partial class Event
{
public Event()
{
ID = Guid.NewGuid();
}
public System.Guid ID { get; set; }
public int AccountID { get; set; }
public string ExternalID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Location { get; set; }
public Nullable<System.DateTime> StartTime { get; set; }
public Nullable<System.DateTime> EndTime { get; set; }
public Nullable<bool> AllDay { get; set; }
public Nullable<System.Guid> ImageData { get; set; }
public string ImageUrl { get; set; }
public virtual Account Account { get; set; }
public virtual FetchedFile FetchedFile { get; set; }
}

Resources