C
C#•2mo ago
Azim

Entity Framework Foreign Key restraint

Hi all, I'm trying to make an appointment scheduling system. The appointment section has its own database linked to a patients model and an appointment model. The error happens when I try save a new appointment -
_appointmentContext.SaveChanges();
_appointmentContext.SaveChanges();
These are my models:
public class AppointmentDbContext : DbContext
{
public DbSet<Appointment> Appointments { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Set the file location for the patient database
optionsBuilder.UseSqlite("Data Source=appointment.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Appointment>()
.HasOne(a => a.Patient)
.WithMany(p => p.Appointments) // Matches Patient.Appointments
.HasForeignKey(a => a.PatientId)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Appointment>()
.HasOne(a => a.User)
.WithMany()
.HasForeignKey(a => a.UserId)
.OnDelete(DeleteBehavior.Restrict);
}

public enum AppointmentStatus
{
Completed = 1,
Scheduled = 2,
Cancelled = 3,


} //UserAccountType

public class Appointment
{
public int Id { get; set; }
public DateTime Scheduled { get; set; }
public AppointmentStatus Status { get; set; }

//navigation properties
public int PatientId { get; set; }
public Patient Patient { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
}
}
public class AppointmentDbContext : DbContext
{
public DbSet<Appointment> Appointments { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Set the file location for the patient database
optionsBuilder.UseSqlite("Data Source=appointment.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Appointment>()
.HasOne(a => a.Patient)
.WithMany(p => p.Appointments) // Matches Patient.Appointments
.HasForeignKey(a => a.PatientId)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Appointment>()
.HasOne(a => a.User)
.WithMany()
.HasForeignKey(a => a.UserId)
.OnDelete(DeleteBehavior.Restrict);
}

public enum AppointmentStatus
{
Completed = 1,
Scheduled = 2,
Cancelled = 3,


} //UserAccountType

public class Appointment
{
public int Id { get; set; }
public DateTime Scheduled { get; set; }
public AppointmentStatus Status { get; set; }

//navigation properties
public int PatientId { get; set; }
public Patient Patient { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
}
}
5 Replies
Azim
AzimOP•2mo ago
public class PatientDbContext : DbContext
{
public DbSet<Patient> Patients { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Set the file location for the patient database
optionsBuilder.UseSqlite("Data Source=patients.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasMany(p => p.Appointments)
.WithOne(a => a.Patient)
.HasForeignKey(a => a.PatientId)
.OnDelete(DeleteBehavior.Cascade);



}
}
public class Patient
{
public int Id { get; set; } //Unique ID
public string FirstName { get; set; }
public string Surname { get; set; }
public DateTime DateOfBirth { get; set; }
public string ContactDetail { get; set; }
public string NHSNumber { get; set; }
public string HospitalNumber { get; set; }
public string HospitalRecords { get; set; }

public ICollection <Appointment> Appointments { get; set; }

} // class patients
} //namespace
public class PatientDbContext : DbContext
{
public DbSet<Patient> Patients { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Set the file location for the patient database
optionsBuilder.UseSqlite("Data Source=patients.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasMany(p => p.Appointments)
.WithOne(a => a.Patient)
.HasForeignKey(a => a.PatientId)
.OnDelete(DeleteBehavior.Cascade);



}
}
public class Patient
{
public int Id { get; set; } //Unique ID
public string FirstName { get; set; }
public string Surname { get; set; }
public DateTime DateOfBirth { get; set; }
public string ContactDetail { get; set; }
public string NHSNumber { get; set; }
public string HospitalNumber { get; set; }
public string HospitalRecords { get; set; }

public ICollection <Appointment> Appointments { get; set; }

} // class patients
} //namespace
Angius
Angius•2mo ago
Why do you have two separate databases in the first place? Cross-database relationships are not really a thing
Azim
AzimOP•2mo ago
I realised after, first time dealing with database's and entity
Angius
Angius•2mo ago
Yeah, so, generally speaking you want to have a single database. It's easier this way DbContext represents the whole database DbSets within it, represent tables And the individual entities describe the columns of those tables
Azim
AzimOP•2mo ago
Fixed all of it, don't have any errors 🙂 thank you
Want results from more Discord servers?
Add your server