title: Automapper entity framework foreign key is null
body: <p>I am trying to update the database using entity framework, I map my entities to viewmodels using automapper, and map it back the same way:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Edit([FromJson] MyCVViewModel model)
  {
      var userId = User.Identity.GetUserId();
      //find the cv
      CV cv = repository.FindCV(model.CVId);
  
      //auto mapper mapping
      Mapper.CreateMap&lt;MyCVViewModel, CV&gt;();
      Mapper.CreateMap&lt;MyCompanyViewModel, Company&gt;();
      cv = Mapper.Map&lt;MyCVViewModel, CV&gt;(model, cv);
  
      //edit
      repository.EditCV(cv);
  }
  </code></pre>
  
  <p>When I map it back, the foreign key CVid inside company entity becomes 0, i think something was lost during the mapping process, how do you map the foreign key?</p>
  
  <p><img src="https://i.stack.imgur.com/6qGxI.png" alt="enter image description here"></p>
  
  <p>Here is my view model and entity:</p>
  
  <p>View Model:</p>
  
  <pre><code>public class MyCVViewModel
  {
      public int CVId { get; set; }
  
      [Required]
      [StringLength(100, ErrorMessage = "Title cannot exceed 100 characters.")]
      [Display(Name = "Title")]
      public string Title { get; set; }
      [Required]
      [StringLength(1000, ErrorMessage = "Statment cannot exceed 1000 characters.")]
      [Display(Name = "Statement")]
      public string Statement { get; set; }
  
      public bool Reference { get; set; }
  
      public List&lt;MyCompanyViewModel&gt; Companies { get; set; }
  }
  
  public class MyCompanyViewModel
  {
      [Required]
      [StringLength(100, ErrorMessage = "Company Name cannot exceed 100 characters.")]
      [Display(Name = "Company Name")]
      public string CompanyName { get; set; }
      [Required]
      [StringLength(100, ErrorMessage = "Job Title cannot exceed 100 characters.")]
      [Display(Name = "Job Title")]
      public string JobTitle { get; set; }
      [Required]
      [DataType(DataType.Date)]
      [Display(Name = "Start Date")]
      public DateTime StartDate { get; set; }
      [Required]
      [DataType(DataType.Date)]
      [Display(Name = "End Date")]
      public DateTime EndDate { get; set; }
      [Required]
      [StringLength(1000, ErrorMessage = "Job Description cannot exceed 1000 characters.")]
      [Display(Name = "Job Description")]
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>Entity:</p>
  
  <pre><code>public class CV
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int CVId { get; set; }
      public string Title { get; set; }
      public string Statement { get; set; }
      public bool Reference { get; set; }
  
      public virtual ICollection&lt;Company&gt; Companies { get; set; }
  }
  
  public class Company
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int CompanyId { get; set; }
  
      public string CompanyName { get; set; }
      public string JobTitle { get; set; }
      public DateTime StartDate { get; set; }
      public DateTime EndDate { get; set; }
      public string Description { get; set; }
  
      public virtual CV CV { get; set; }
      public int CVId { get; set; }
  }
  </code></pre>
  
  <p>and this is the error message when I try to update:</p>
  
  <p><strong>The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value.</strong></p>
  
  <p>I see where the problem is, but don't know how to tell automapper to retain foreign key value</p>