C
C#2y ago
bookuha

DBFirst one to one or zero

I have One Patient to One or No MedicalCard associated with him EF Generated the following code:
public Patient(){
{
Appointments = new HashSet<Appointment>();
MedicalCards = new HashSet<MedicalCard>();
}

public int Id { get; set; }
public string? Lastname { get; set; }
public string? Firstname { get; set; }
public string? Middlename { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public bool? Gender { get; set; }
public int? Medcard { get; set; }

public virtual MedicalCard? MedcardNavigation { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
public virtual ICollection<MedicalCard> MedicalCards { get; set; }

public partial class MedicalCard
{
public MedicalCard()
{
MedicalReports = new HashSet<MedicalReport>();
Patients = new HashSet<Patient>();
}
public int Id { get; set; }
public int Patient { get; set; }
public DateTime? Issued { get; set; }

public virtual Patient PatientNavigation { get; set; } = null!;
public virtual ICollection<MedicalReport> MedicalReports { get; set; }
public virtual ICollection<Patient> Patients { get; set; }
}
public Patient(){
{
Appointments = new HashSet<Appointment>();
MedicalCards = new HashSet<MedicalCard>();
}

public int Id { get; set; }
public string? Lastname { get; set; }
public string? Firstname { get; set; }
public string? Middlename { get; set; }
public string? Phone { get; set; }
public string? Address { get; set; }
public bool? Gender { get; set; }
public int? Medcard { get; set; }

public virtual MedicalCard? MedcardNavigation { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
public virtual ICollection<MedicalCard> MedicalCards { get; set; }

public partial class MedicalCard
{
public MedicalCard()
{
MedicalReports = new HashSet<MedicalReport>();
Patients = new HashSet<Patient>();
}
public int Id { get; set; }
public int Patient { get; set; }
public DateTime? Issued { get; set; }

public virtual Patient PatientNavigation { get; set; } = null!;
public virtual ICollection<MedicalReport> MedicalReports { get; set; }
public virtual ICollection<Patient> Patients { get; set; }
}
Why did it create Collections? What's the use of them? I have id and single entity navigation property, but why the collections?
6 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2y ago
Ooof ouch owie db-first
bookuha
bookuha2y ago
should i rewrite it to code first? what would be the fastest way?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2y ago
Code-first is definitely easier to deal with IME
bookuha
bookuha2y ago
i dont really care about the data, just want my services to keep up well with new code first entities like, i dont use these collections anywhere anyway so i just need to drop the db, clear migrations, remove redundant fields, specify correct one to one...zero and i hope it will work fine