title: One to One Relationship with Different Primary Key in EF 6.1 Code First
body: <p>I am having an issue getting a reference to the employee object from the PayGroup object using Entity Framework 6.1. I have a foreign key in the database on PayGroup.SupervisorId -> Employee.EmployeeId. Note that this is a zero or one-to-one relationship (a pay group can only have one supervisor and an employee can only be the supervisor of one pay group).</p>
  
  <p>According to <a href="https://github.com/aspnet/EntityFramework6/issues/159" rel="nofollow noreferrer">this post on GitHub</a>, it is not possible to have a foreign key on a table with a different primary key. I've added the foreign key to the database manually but I can't figure out how to set up the fluent api mapping to be able to get the employee object from pay group.</p>
  
  <p><strong>Pay Group Table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Cclop.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cclop.png" alt="Pay Group Table"></a></p>
  
  <p><strong>Employee Table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/kd5nA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kd5nA.png" alt="Employee Table"></a></p>
  
  <p><em>Note: There is a foreign key from PayGroup.SupervisorId - Employee.EmployeeId in the database.</em></p>
  
  <p>Below are the DTO's (I don't currently have any working relationship mapping between these classes):</p>
  
  <pre><code>public class PayGroup
  {
      public int Id { get; set; }
      public string SupervisorId { get; set; }
      public virtual Employee Supervisor { get; set; }
  }
  
  public class Employee
  {
      public string EmployeeId { get; set; }
      public string FullName { get; set; }
  }
  </code></pre>