I am doing a little .NET application to get use to it but I struggle to understand something.
When I pass through the following line of code the parameterless constructor of my Historique class is called.
public void AjouteEvent(int societeId, Evenement evnmt)
{
Historique histo = _db.Historiques.Where(h => h.ID ==societeId).FirstOrDefault(); // Called here
histo.Evenements.Add(evnmt);
_db.SaveChanges();
}
With the following _db object :
private BddContext _db;
public Dal()
{
_db = new BddContext();
}
The Historique class :
public class Historique
{
[ForeignKey("Societe")]
public int ID { get; set; }
public Societe Societe { get; set; }
public List<Evenement> Evenements { get; set; }
public Historique()
{
Evenements = new List<Evenement>();
Evenements.Add(new Evenement());
}
}
And the Evenement class :
public class Evenement
{
public int ID { get; set; }
public DateTime DateCreation { get; set; } = DateTime.Now;
[Display(Name = "Date limite"), Required(ErrorMessage = "Veuillez préciser une date limite pour terminer l'évènement.")]
public DateTime DateLimite { get; set; } = DateTime.Now;
public string Type { get; set; } = "Création";
public bool Actif { get; set; } = true;
public int HistoriqueId { get; set; }
public Historique Historique { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder(this.Type);
sb.Append($" / {this.Actif}");
return sb.ToString();
}
}
Thanks for your answers :) !