I have patient,appointment and doctor tables on EntityFramework and their dbset also exist on dbcontext.Basically patient can book appointment from any doctor which means appointment should have doctor and userId also model doctor and user should have appointment collection which means many to many relationship.
Patient,Doctor,Appointment models below
public class Patient
{
public int Id { get; set; }
public string IdentityNumber { get; set; }
public string Name { get; set; }
public byte[] PasswordHash { get; set; }
public byte[] PasswordSalt { get; set; }
public DateTime BirthDate { get; set; }
public string Email { get; set; }
public ICollection<Appointment> Appointments { get; set; }
}
public class Doctor
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public Department Department { get; set; }
public int DepartmentId { get; set; }
public ICollection<Appointment> Appointments { get; set; }
}
public class Appointment
{
public int Id { get; set; }
public DateTime AppointmentDate { get; set; }
public bool Status { get; set; }
public Doctor Doctor { get; set; }
public int DoctorId { get; set; }
public Patient Patient { get; set; }
public int PatientId { get; set; }
}
As far as I know,dbcontext should be modified for many to many relationships on EntityFramework.Here what I tried below for many to many relationship on dbcontext
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Patient> Patients { get; set; }
public DbSet<Appointment> Appointments { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Appointment>()
.HasKey(a => new { a.DoctorId , a.PatientId });
builder.Entity<Appointment>()
.HasOne(a => a.Doctor)
.WithMany(a => a.Appointments)
.HasForeignKey(a => a.DoctorId);
builder.Entity<Appointment>()
.HasOne(a => a.Patient)
.WithMany(a => a.Appointments)
.HasForeignKey(a => a.PatientId);
}
I recieve this error after dotnet run command
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'FOREIGN KEY constraint failed'.
PatientSeed.json
[
{
"IdentityNumber": "21907141860",
"Name": "Ali",
"BirthDate": "2017-02-07T10:04:33 -03:00",
"Email": "timucininmaili@gmail.com",
"Appointments": [
{
"AppointmentDate": "2017-02-07T10:04:33 -03:00",
"Status": false
}
]
},
{
"IdentityNumber": "12906141850",
"Name": "Veli",
"BirthDate": "2017-02-07T10:04:33 -03:00",
"Email": "timucininmaili@gmail.com",
"Appointments": [
{
"AppointmentDate": "2017-02-07T10:04:33 -03:00",
"Status": false
}
]
},
{
"IdentityNumber": "22605131860",
"Name": "Timucin",
"BirthDate": "2017-02-07T10:04:33 -03:00",
"Email": "timucininmaili@gmail.com",
"Appointments": [
{
"AppointmentDate": "2017-02-07T10:04:33 -03:00",
"Status": false
}
]
}
]
Seed.cs
public static void SeedUsers(DataContext context)
{
var patientData = System.IO.File.ReadAllText("Data/PatientSeed.json");
var patients = JsonConvert.DeserializeObject<List<Patient>>(patientData);
foreach (var patient in patients)
{
byte[] passwordhash, passwordSalt;
CreatePasswordHash("password", out passwordhash, out passwordSalt);
patient.PasswordHash = passwordhash;
patient.PasswordSalt = passwordSalt;
patient.Name = patient.Name.ToLower();
context.Patients.Add(patient);
}
context.SaveChanges();
}
}
private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
{
using (var hmac = new System.Security.Cryptography.HMACSHA512())
{
passwordSalt = hmac.Key;
passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
}
}
User contributions licensed under CC BY-SA 3.0