title: Entity Framework 6.1.3 Foreign Key on non primary key field
body: <p>I have the following 2 classes:</p>
  
  <pre><code>public class UserMembership: Entity
  {
      public Guid Id { get; set; }
      public Guid UserGroupId { get; set; }
      public string LoginId { get; set; }
      public Guid SiteId { get; set; }
  
      public virtual Site Site { get; set; }
      public virtual SecurityUser SecurityUser { get; set; }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class SecurityUser: Entity
  {
      ICollection&lt;UserMembership&gt; _userMemberships = new Collection&lt;UserMembership&gt;();
  
      public Guid Id { get; set; }
      public string LoginId { get; set; }
      public string DisplayName { get; set; }
      public DateTimeOffset? LastLogOn { get; set; }
      public bool IsActive { get; set; }
      public bool IsServiceAccount { get; set; }
      public bool IsUserAccount { get; set; }
  
      public virtual ICollection&lt;UserMembership&gt; UserMemberships 
      {
          get { return _userMemberships; }
          set { _userMemberships = value; }
      }
  }
  </code></pre>
  
  <p>And want to join UserMembership and SecurityUser using the LoginId fields NOT the primary key.  Is this possible using the Fluent API and not attributes?</p>
  
  <p>Here are the mapping classes:</p>
  
  <pre><code>public class SecurityUserMap : EntityMap&lt;SecurityUser&gt;
  {
      public SecurityUserMap()
      {
          HasKey(p =&gt; p.Id);
  
          Property(p =&gt; p.LoginId)
              .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute{ IsClustered = true, IsUnique = true }))
              .HasMaxLength(SecurityUserValidator.MaxLoginIdLength).IsRequired();
          Property(p =&gt; p.DisplayName)
              .HasMaxLength(SecurityUserValidator.MaxDisplayNameLength).IsRequired();
  
          Property(p =&gt; p.Modified)
              .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute()));
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class UserMembershipMap: EntityMap&lt;UserMembership&gt;
  {
      public UserMembershipMap()
      {
          HasKey(p =&gt; p.Id);
  
          Property(p =&gt; p.UserGroupId)
              .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UK_UserMembership", 1) { IsClustered = true, IsUnique = true }));
          Property(p =&gt; p.LoginId)
              .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UK_UserMembership", 2) { IsClustered = true, IsUnique = true }))
              .HasMaxLength(UserMembershipValidator.MaxLoginIdLength).IsRequired();
          Property(p =&gt; p.SiteId)
              .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UK_UserMembership", 3) { IsClustered = true, IsUnique = true }));
  
          Property(p =&gt; p.Modified)
              .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute()));
  
          HasRequired(p =&gt; p.SecurityUser).WithMany(p =&gt; p.UserMemberships).Map(t =&gt; t.MapKey(new[] { "LoginId" }));
      }
  }
  </code></pre>