title: How to Add records in DB with foreign key relatationship using .Net core web Api and EF core
body: <p>I am developing Web Api CRUD repository pattern using multiple tables with Primary and Foreign key relation and for each table while inserting using separate [HttpPost] method. But how to insert(post) the records in child tables with foreign key Id property.</p>
  <p>EmpVaccineDetail Model :</p>
  <pre><code>    public partial class EmpVaccineDetail
      {
      public EmpVaccineDetail()
      {
          CovidInfectionDetails = new HashSet&lt;CovidInfectionDetail&gt;();
          VaccineDoses = new HashSet&lt;VaccineDose&gt;();
      }
  
      public int EmpVaccineDetailId { get; set; }
      public string VaccineType { get; set; }
      public int UniqueHealthId { get; set; }
      public int BeneficiaryId { get; set; }
      public string VaccinationStatus { get; set; }
      public bool DoYouWantToReportCovid { get; set; }
  
      public virtual ICollection&lt;CovidInfectionDetail&gt; CovidInfectionDetails { get; set; }
      public virtual ICollection&lt;VaccineDose&gt; VaccineDoses { get; set; }
  }
  </code></pre>
  <p>VaccineDose Model :</p>
  <pre><code>    public partial class VaccineDose
      {
      public int VaccineDoseId { get; set; }
      public DateTime DateOfVaccine { get; set; }
      public string VaccineOrganizedBy { get; set; }
      public bool? Reimbursed { get; set; }
      public Guid? CoWinCertificateUploadFileId { get; set; }
      public string HospitalName { get; set; }
      public Guid? ReceiptUploadFileId { get; set; }
      public string VaccineDoseType { get; set; }
      [ForeignKey]
      public int EmpVaccineDetailId { get; set; }
  
      public virtual EmpVaccineDetail EmpVaccineDetail { get; set; }
  }
  </code></pre>
  <p>VaccineDetailsRepository :</p>
  <pre><code>    public class VaccineDetailsRepository : IVaccineDetails
     {
      private readonly PeopleWorksContext _peopleWorksContext;
      public VaccineDetailsRepository(PeopleWorksContext peopleWorksContext)
      {
          _peopleWorksContext = peopleWorksContext;
      }
  
      public async Task&lt;EmpVaccineDetail&gt; Add(EmpVaccineDetail empVaccineDetail)
      {
          await _peopleWorksContext.EmpVaccineDetails.AddAsync(empVaccineDetail);
          await _peopleWorksContext.SaveChangesAsync();
          return empVaccineDetail;
      }
     }
  </code></pre>
  <p>VaccineDosesRepository :</p>
  <pre><code>    public class VaccineDosesRepository : IVaccineDoses
  {
      private readonly PeopleWorksContext _peopleWorksContext;
      public VaccineDosesRepository(PeopleWorksContext peopleWorksContext)
      {
          _peopleWorksContext = peopleWorksContext;
      }
      public async Task&lt;VaccineDose&gt; Add(VaccineDose vaccineDose)
      {
          await _peopleWorksContext.VaccineDoses.AddAsync(vaccineDose);
          await _peopleWorksContext.SaveChangesAsync();
          return vaccineDose;
      }
  }
  </code></pre>
  <p>Controller :</p>
  <pre><code>    public class VaccineDetailsController : ControllerBase
      {
      private readonly IVaccineDetails _vaccineDetails;
      private readonly IVaccineDoses _vaccineDoses;
      public VaccineDetailsController(IVaccineDetails vaccineDetails, IVaccineDoses 
      vaccineDoses)
      {
          _vaccineDetails = vaccineDetails;
          _vaccineDoses = vaccineDoses;
      }
  
      [HttpPost]
      [Route(&quot;VaccineDetails/AddVaccineDetail&quot;)]
      public async Task&lt;IActionResult&gt; AddVaccineDetail(EmpVaccineDetail empVaccineDetail)
      {
          try
          {
              await _vaccineDetails.Add(empVaccineDetail);
              return Ok();
          }
          catch (Exception ex)
          {
              return StatusCode(StatusCodes.Status500InternalServerError, ex);
          }
      }
  
      [HttpPost]
      [Route(&quot;VaccineDetails/AddVaccineDose&quot;)]
      public async Task&lt;IActionResult&gt; AddVaccineDose(VaccineDose vaccineDose)
      {
          try
          {
              await _vaccineDoses.Add(vaccineDose);
              return Ok();
          }
          catch (Exception ex)
          {
              return StatusCode(StatusCodes.Status500InternalServerError, ex);
          }
      }
     }
  </code></pre>
  <p>I added the data in the 1st table but while inserting in the 2nd table how do i pass the foreign key property. Please suggest. Thanks</p>