如何从 ASP.NET Core 3.1 Identity 中的 IdentityUser 类添加关系? 代码优先

问题描述

我有一个 Asp.Net Core MVC 3.1 项目。我想使用代码优先技术加入预约表和患者表,但外键列为空。

我的病人班;

public class Patient : IdentityUser
{
     [NotMapped]
     public List<Appointment> Appointments { get; set; }
}

还有我的Appointment课,我和病人和预约是一对多的关系。

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;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...