I have a Asp.Net Core MVC 3.1 project. I want to join appointment and patient tables with code first technique however the foreign key column is null.
My Patient Class;
public class Patient : IdentityUser
{
[NotMapped]
public List<Appointment> Appointments { get; set; }
}
And my Appointment class, I have one to many relationship with patient and appointment.
public class Appointment
{
[Key]
public int ID { get; set; }
public string PatientID { get; set; }
public Patient Patient { get; set; }
}
If I add "required" annotations to the foreign key not saving appointment.
public Result<AppointmentVM> CreateAppointment(AppointmentVM model)
{
if (model != null)
{
try
{
var appointment = _mapper.Map<AppointmentVM, Appointment>(model);
_unitOfWork.appointmentRepository.Add(appointment);
_unitOfWork.Save();
return new Result<AppointmentVM>(true, "Successful");
}
catch (Exception ex)
{
return new Result<AppointmentVM>(false, "Error" + ex.Message.ToString());
}
}
else
return new Result<AppointmentVM>(false, "Not null");
}
How can i solve this problem?
public class AppointmentVM
{
public int AppointmentID { get; set; }
public string PatientID { get; set; }
public PatientVM Patient { get; set; }
}
public class AppointmentRepository : Repository<Appointment>, IAppointmentRepository
{
private readonly ClinicContext _ctx;
public AppointmentRepository(ClinicContext ctx) : base(ctx)
{
_ctx = ctx;
}
}