Cześć,
posługuję się pierwszy raz Entity Framework i coś mi nie wychodzi:
mam klasę Pacjent (Patient), każdy pacjent ma 52 zęby (Teeth), listę zabiegów (itsTreatments), listę przyszłych zabiegów i listę chorób:
abstract public class Person
{
public string itsName { get; set; }
public string itsSurname { get; set; }
public string itsPESEL { get; set; }
public Address itsAddress { get; set; }
public string itsTelephone { get; set; }
public string itsBirthDate { get; set; }
}
public class Patient : Person
{
public int PatientId { get; set; }
private Tooth[] itsTeeth;
public List<Treatment> itsTreatments { get; set; }
public List<Treatment> itsTreatmentPlans { get; set; }
public List<Disease> itsDiseases { get; set; }
public Patient() { }
public Patient(string name, string surname, string pesel, Address address, string tel, string birth)
{
itsName = name;
itsSurname = surname;
itsPESEL = pesel;
itsAddress = address;
itsTelephone = tel;
itsBirthDate = birth;
itsTreatments = new List<Treatment>();
itsTreatmentPlans = new List<Treatment>();
itsTeeth = new Tooth[52] { new Tooth("18"), new Tooth("17"), new Tooth("16"), new Tooth("15"), new Tooth("14"), new Tooth("13"), new Tooth("12"), new Tooth("11"),
new Tooth("28"), new Tooth("27"), new Tooth("26"), new Tooth("25"), new Tooth("24"), new Tooth("23"), new Tooth("22"), new Tooth("21"),
new Tooth("38"), new Tooth("37"), new Tooth("36"), new Tooth("35"), new Tooth("34"), new Tooth("33"), new Tooth("32"), new Tooth("31"),
new Tooth("48"), new Tooth("47"), new Tooth("46"), new Tooth("45"), new Tooth("44"), new Tooth("43"), new Tooth("42"), new Tooth("41"),
new Tooth("55"), new Tooth("54"), new Tooth("53"), new Tooth("52"), new Tooth("51"),
new Tooth("65"), new Tooth("64"), new Tooth("63"), new Tooth("62"), new Tooth("61"),
new Tooth("75"), new Tooth("74"), new Tooth("73"), new Tooth("72"), new Tooth("71"),
new Tooth("85"), new Tooth("84"), new Tooth("83"), new Tooth("82"), new Tooth("81") };
}
}
public class Tooth
{
public int ToothId { get; set; }
public string ToothPosition { get; }
private bool _hasCrown;
private bool _isEndo;
private bool _isExtracted;
private bool _isCutting;
private bool _isStopped;
public virtual Patient itsPatient { get; set; }
}
Jak widać, konstruktor klasy Patient tworzy tablicę 52 zębów. Klasa DbContext wygląda tak:
class PatientsDatabaseContext : DbContext
{
public DbSet<Patient> Pacjenci { get; set; }
public DbSet<Tooth> Teeth { get; set; }
public DbSet<Disease> Diseases { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasMany<Disease>(s => s.itsDiseases).WithMany();
modelBuilder.Entity<Patient>()
.HasMany<Treatment>(s => s.itsTreatments).WithOptional();
modelBuilder.Entity<Patient>()
.HasMany<Treatment>(s => s.itsTreatmentPlans).WithOptional();
}
}
Tworzę nowego pacjenta za pomocą ww. konstruktora i dołączam do bazy:
Database = new PatientsDatabaseContext();
Database.Pacjenci.Add(nowyPacjent);
Database.SaveChanges();
Pacjent, tj. jego imię, adres itd. się dodaje, ale zęby się nie dodają. Przy ponownej próbie wczytania danych gdy np. wywołam funkcję itsTreatments.Count() powstaje ArgumentNullException - obiekt itsTreatments, Teeth, nie istnieją w pacjencie.
Co robię nie tak?