简体   繁体   English

EF Core 一对多关系实例化 Object 内部 Object 构造函数

[英]EF Core One To Many Relationship Instantiate Object Inside Object Constructor

I am very new to C# and EF Core.我对 C# 和 EF Core 非常陌生。 I have the following entity classes: One Doctor has many Patients.我有以下实体类:一个医生有很多病人。 I am trying to create/post a new patient in Postman, and the Doctor property is required.我正在尝试在 Postman 中创建/发布新患者,并且需要 Doctor 属性。 When I create a patient and pass a DoctorId to be used for instantiating a patient, is there a way to get the DoctorId that is being passed to also instantiate the Doctor property (object) within the constructor at the same time?当我创建一个患者并传递一个用于实例化患者的 DoctorId 时,有没有办法让传递的 DoctorId 同时在构造函数中实例化 Doctor 属性(对象)? Basically, I need to save the existing instance of the Doctor using the DoctorId being passed in to create a patient as well.基本上,我还需要使用传入的 DoctorId 来保存现有的 Doctor 实例以创建患者。 Is there a way to do this?有没有办法做到这一点?

I need this piece of code (in Patient.cs) to tell Entity Framework that Doctor and Patient has one to many relationship when I add migration.我需要这段代码(在 Patient.cs 中)告诉 Entity Framework,当我添加迁移时,Doctor 和 Patient 具有一对多的关系。

public Doctor Doctor { get; set; } = new Doctor();

Here are the model classes:以下是 model 类:

Doctor.cs:医生.cs:

public class Doctor : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<Patient>? Patients { get; set; }

    public Doctor()
    {

    }

    public Doctor(string firstName, string lastName, ICollection<Patient>? patients)
    {
        FirstName = firstName;
        LastName = lastName;
        Patients = patients;
    }
}

Patient.cs病人.cs

public class Patient
{
    public int PatientId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public int Age => this.CalculateAge(DateOfBirth);
    public Gender Gender { get; set; }
    public string DoctorId { get; set; }
    public Doctor Doctor { get; set; }
    public ICollection<Diagnosis>? Diagnoses { get; set; }
    
    //Get doctor id being passed and save it as doctor object

    public Patient()
    {
    }

    public Patient(int patientId, string firstName, string lastName, DateTime dateOfBirth, Gender gender, Doctor doctor, string doctorId, ICollection<Diagnosis>? diagnoses)
    {
        PatientId = patientId;
        FirstName = firstName;
        LastName = lastName;
        DateOfBirth = dateOfBirth;
        Gender = gender;
        Doctor = doctor;
        DoctorId = doctorId;
        Diagnoses = diagnoses;
    }

What if you inject the context and just query the db with that id?如果您注入上下文并仅使用该 ID 查询数据库会怎样? im sure this helps you:) https://docs.microsoft.com/en-us/ef/core/modeling/constructors我相信这对你有帮助:) https://docs.microsoft.com/en-us/ef/core/modeling/constructors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM