我有一个 Asp.Net Core MVC 3.1 项目。我想使用代码优先技术加入约会和患者表,但是外键列为空。
我的病人班;
public class Patient : IdentityUser
{
[NotMapped]
public List<Appointment> Appointments { get; set; }
}
而我的预约课,我与病人和预约有一对多的关系。
public class Appointment
{
[Key]
public int ID { get; set; }
public string PatientID { get; set; }
public Patient Patient { get; set; }
}
如果我向外键添加“必需”注释而不保存约会。
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");
}
我怎么解决这个问题?
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;
}
}