Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何插入包含doctorID和patientID的.json数据的约会表?_C#_Database_Entity Framework_Asp.net Core - Fatal编程技术网

C# 如何插入包含doctorID和patientID的.json数据的约会表?

C# 如何插入包含doctorID和patientID的.json数据的约会表?,c#,database,entity-framework,asp.net-core,C#,Database,Entity Framework,Asp.net Core,我想在我的预约、医生、病人表之间建立关系。医生来自科室和医院下的科室。首先使用.NET EF代码对我来说似乎有点复杂 模范医生 public class Doctor { public int Id { get; set; } public string Name { get; set; } public string Title { get; set; } public Department Department { ge

我想在我的预约、医生、病人表之间建立关系。医生来自科室和医院下的科室。首先使用.NET EF代码对我来说似乎有点复杂

模范医生

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

    }
预约基本上属于医生和患者(不必为空)。如果任何患者尚未预约,则patientID null else患者ID将等于预约的患者ID。对于医生ID,也将相同

我想用数据启动应用程序,所以这些是我在下面创建的约会json

[
  {
    "IdentityNumber": "21907141860",
    "Name": "Ali",
    "BirthDate": "2017-02-07T10:04:33 -03:00",
    "Email": "timucininmaili@gmail.com"
  },
  {
    "IdentityNumber": "12906141850",
    "Name": "Veli",
    "BirthDate": "2017-02-07T10:04:33 -03:00",
    "Email": "timucininmaili@gmail.com"

  },
  {
    "IdentityNumber": "22605131860",
    "Name": "Timucin",
    "BirthDate": "2017-02-07T10:04:33 -03:00",
    "Email": "timucininmaili@gmail.com"
  }
]
但是我不知道如何将这些预约映射到预约表上的DoctorID和PatientID。我应该添加预约下的医生和患者的对象数组还是其他我不知道的东西

预期产出 AppointmentID DoctorID PatientID(如果不存在,则为空)-预约日期

实际产出 没有预约。所有列都为空(但医生和患者表是使用.json成功创建的)

Json医院下的医生填充示例

[
  {
    "Name": "Concetta Hospital",
    "Location": "United States",
    "Departments": [
      {
        "name": "Eye",
        "Doctors": [
          {
            "name": "Sean Paul",
            "title": "Eye Doctor"
          }
        ]
填充患者的Json示例

  {
    "IdentityNumber": "12906141850",
    "Name": "Veli",
    "BirthDate": "2017-02-07T10:04:33 -03:00",
    "Email": "timucininmaili@gmail.com"
  }
Appointment.Json

   {
        "AppointmentDate": "2017-02-07T10:04:33 -03:00",
        "Status": true
      },
      {
        "AppointmentDate": "2016-02-07T10:04:33 -03:00",
        "Status": true
      },
      {
        "AppointmentDate": "2018-02-07T10:04:33 -03:00",
        "Status": true
      }
 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);
        }

        var appointmentData = System.IO.File.ReadAllText("Data/AppointmentSeed.json");
        var appointments = JsonConvert.DeserializeObject<List<Appointment>>(appointmentData);
        foreach (var appointment in appointments)
        {
            context.Appointments.Add(appointment);
        }

        context.SaveChanges();
Seed.cs(迁移后填充表的方式)

var patientData=System.IO.File.ReadAllText(“Data/PatientSeed.json”);
var patients=JsonConvert.DeserializeObject(patientData);
foreach(患者中的var患者)
{
字节[]passwordhash,passwordSalt;
CreatePasswordHash(“password”,out passwordhash,out passwordSalt);
patient.PasswordHash=PasswordHash;
patient.PasswordSalt=PasswordSalt;
patient.Name=patient.Name.ToLower();
context.Patients.Add(患者);
}
var appointmentData=System.IO.File.ReadAllText(“Data/AppointmentSeed.json”);
var appointments=JsonConvert.DeserializeObject(appointmentData);
foreach(预约中的var预约)
{
上下文。约会。添加(约会);
}
SaveChanges();

如果您想将这些预约与预约表上的DoctorID和PatientID进行映射,您应该在appointment.json中包含医生和患者的相关信息。下面是一个演示:

  • Appointment.Json

    [
     {
      "AppointmentDate": "2017-02-07T10:04:33 -03:00",
      "Status": true,
      "Type": "Eye",
      "PatientIdentityName": "21907141860",
      "DoctorName": "Sean Paul"
     },
    {
     "AppointmentDate": "2016-02-07T10:04:33 -03:00",
     "Status": true,
     "Type": "Tooth",
     "PatientIdentityName": "12906141850",
     "DoctorName": "Aisha Ni"
    },
    {
     "AppointmentDate": "2018-02-07T10:04:33 -03:00",
     "Status": true,
     "Type": "Eye",
     "PatientIdentityName": "22605131860",
     "DoctorName": "Daniel Li"
    }
    ]
    
  • 2.对约会进行建模,并使用AppointmentViewModel获取jsondata

    public class Appointment
    {
        public int Id { get; set; }
        public DateTime AppointmentDate { get; set; }
        public bool Status { get; set; }
        public int DoctorId { get; set; }
    
        [ForeignKey("DoctorId")]
        public Doctor Doctor { get; set; }
    
        public int PatientId { get; set; }
        [ForeignKey("PatientId")]
        public Patient Patient { get; set; }
    }
    
    public class AppointmentViewModel
    {
        public DateTime AppointmentDate { get; set; }
        public bool Status { get; set; }
        public string Type { get; set; }
        public string PatientIdentityName { get; set; }
        public string DoctorName { get; set; }
    }
    
    3.数据种子

    var appointmentData = System.IO.File.ReadAllText("Data/AppointmentSeed.json");
                var appointmentVMs = JsonConvert.DeserializeObject<List<AppointmentViewModel>>(appointmentData);
                foreach (var appointment in appointmentVMs)
                {
                    var departmentId = context.Departments.SingleOrDefault(d => d.Name == appointment.Type).Id;
                    Appointment model = new Appointment();
    
                    model.AppointmentDate = appointment.AppointmentDate;
                    model.Status = appointment.Status;
                    model.DoctorId = context.Doctors.SingleOrDefault(d => d.Name == appointment.DoctorName && d.DepartmentId == departmentId).Id;
                    model.PatientId = context.Patients.SingleOrDefault(p => p.IdentityNumber == appointment.PatientIdentityName).Id;
                    context.Appointments.Add(model);
                }
    
                context.SaveChanges();
    
    var appointmentData=System.IO.File.ReadAllText(“Data/AppointmentSeed.json”);
    var appointmentVMs=JsonConvert.DeserializeObject(appointmentData);
    foreach(任命VMS中的var任命)
    {
    var departmentId=context.Departments.SingleOrDefault(d=>d.Name==appointment.Type).Id;
    预约模式=新预约();
    model.AppointmentDate=appointment.AppointmentDate;
    model.Status=约会.Status;
    model.DoctorId=context.Doctors.SingleOrDefault(d=>d.Name==appointment.DoctorName&&d.DepartmentId==DepartmentId).Id;
    model.PatientId=context.Patients.SingleOrDefault(p=>p.IdentityNumber==appointment.PatientIdentityName).Id;
    context.appoints.Add(模型);
    }
    SaveChanges();
    
    如果您想将这些预约与预约表上的DoctorID和PatientID进行映射,您应该在appointment.json中包含医生和患者的相关信息。下面是一个演示:

  • Appointment.Json

    [
     {
      "AppointmentDate": "2017-02-07T10:04:33 -03:00",
      "Status": true,
      "Type": "Eye",
      "PatientIdentityName": "21907141860",
      "DoctorName": "Sean Paul"
     },
    {
     "AppointmentDate": "2016-02-07T10:04:33 -03:00",
     "Status": true,
     "Type": "Tooth",
     "PatientIdentityName": "12906141850",
     "DoctorName": "Aisha Ni"
    },
    {
     "AppointmentDate": "2018-02-07T10:04:33 -03:00",
     "Status": true,
     "Type": "Eye",
     "PatientIdentityName": "22605131860",
     "DoctorName": "Daniel Li"
    }
    ]
    
  • 2.对约会进行建模,并使用AppointmentViewModel获取jsondata

    public class Appointment
    {
        public int Id { get; set; }
        public DateTime AppointmentDate { get; set; }
        public bool Status { get; set; }
        public int DoctorId { get; set; }
    
        [ForeignKey("DoctorId")]
        public Doctor Doctor { get; set; }
    
        public int PatientId { get; set; }
        [ForeignKey("PatientId")]
        public Patient Patient { get; set; }
    }
    
    public class AppointmentViewModel
    {
        public DateTime AppointmentDate { get; set; }
        public bool Status { get; set; }
        public string Type { get; set; }
        public string PatientIdentityName { get; set; }
        public string DoctorName { get; set; }
    }
    
    3.数据种子

    var appointmentData = System.IO.File.ReadAllText("Data/AppointmentSeed.json");
                var appointmentVMs = JsonConvert.DeserializeObject<List<AppointmentViewModel>>(appointmentData);
                foreach (var appointment in appointmentVMs)
                {
                    var departmentId = context.Departments.SingleOrDefault(d => d.Name == appointment.Type).Id;
                    Appointment model = new Appointment();
    
                    model.AppointmentDate = appointment.AppointmentDate;
                    model.Status = appointment.Status;
                    model.DoctorId = context.Doctors.SingleOrDefault(d => d.Name == appointment.DoctorName && d.DepartmentId == departmentId).Id;
                    model.PatientId = context.Patients.SingleOrDefault(p => p.IdentityNumber == appointment.PatientIdentityName).Id;
                    context.Appointments.Add(model);
                }
    
                context.SaveChanges();
    
    var appointmentData=System.IO.File.ReadAllText(“Data/AppointmentSeed.json”);
    var appointmentVMs=JsonConvert.DeserializeObject(appointmentData);
    foreach(任命VMS中的var任命)
    {
    var departmentId=context.Departments.SingleOrDefault(d=>d.Name==appointment.Type).Id;
    预约模式=新预约();
    model.AppointmentDate=appointment.AppointmentDate;
    model.Status=约会.Status;
    model.DoctorId=context.Doctors.SingleOrDefault(d=>d.Name==appointment.DoctorName&&d.DepartmentId==DepartmentId).Id;
    model.PatientId=context.Patients.SingleOrDefault(p=>p.IdentityNumber==appointment.PatientIdentityName).Id;
    context.appoints.Add(模型);
    }
    SaveChanges();
    
    您的预约是什么?您在上面提供的预约json似乎是患者json。@薛丽晨我忘了添加抱歉。我刚刚添加了您可以看到您的预约json是什么?您在上面提供的预约json似乎就是患者json。@XueliChen我忘了添加抱歉。我刚刚添加了您可以看到的内容