title: Entity Framework 4 association with non-key columns/fields and different columns/field names
body: <p>I'm building a new service with EF4 that connects to a legacy DB (that cannot be modified). </p>
  
  <p>(This BD seems to have been built by a DB genius as you'll see... dirty job, I know, but I have to do it)</p>
  
  <p>Basically, I have two tables (<strong>SegurosPassageiros</strong> and <strong>LocsPassageiros</strong>), with their keys that associates with other tables on the regular way. As shown in the below model, they don't have a physical association with PK/FK but they are linked by the non-key columns "<strong>SegurosPassageiros.CodPassageiro</strong>" and "<strong>LocsPassageiros.CodLocPassageiroInterno</strong>". 
  Also, this association is one-to-many:</p>
  
  <p><em>LocsPassageiros 1...</em>* <em>SegurosPassageiros</em></p>
  
  <p>I've found many answers related to non-key association but not both non-keys and different names in the same scenario.</p>
  
  <p>the tables:</p>
  
  <pre><code> LocsPassageiros 
   ----------------------------------
   CodLocPassageiro (PK)   | int
   Nome                    | varchar
   CodLocPassageiroInterno | int
   ----------------------------------
  
  
   ----------------------------------
   SegurosPassageiros
   ----------------------------------
   CodSeguroPassageiro(PK)  | int
   CodPassageiro            | int
   ----------------------------------
  </code></pre>
  
  <p>The code (class "SeguroPassageiro" mapped to table "SegurosPassageiros"):</p>
  
  <pre><code>public class SeguroPassageiro
  {
      [Key]
      public Int32 CodSeguroPassageiro { get; set; }
  
      .... (other fields)
  
       //tried this, no success
      //[ForeignKey("Passageiro")]
      public virtual Int32 CodLocPassageiroInterno { get; set; }
  
       //tried this annotation with no success
      [Association("Passageiro_Seguros", "CodPassageiro", "CodLocPassageiroInterno")]
      public virtual Passageiro Passageiro { get; set; }
  }
  </code></pre>
  
  <p>Class "Passageiro" mapped to table "LocsPassageiros":</p>
  
  <pre><code>public class Passageiro
  {
      [Key]
      public Int32 CodLocPassageiro { get; set; }
  
      ... (other fields)
  
       //tried this, no success
      //[ForeignKey("Seguros")]
      public Int32 CodLocPassageiroInterno { get; set; }
  
      //tried these annotations with no success
      [ForeignKey("CodLocPassageiroInterno")]
      [Association("Passageiro_Seguros", "CodLocPassageiroInterno", "CodPassageiro")]
      public List&lt;SeguroPassageiro&gt; Seguros { get; set; }
  }
  </code></pre>
  
  <p>Setting up the model:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
  
          modelBuilder.Entity&lt;Dominio.Aereo.Passageiro&gt;().ToTable("LocsPassageiros");
          modelBuilder.Entity&lt;Dominio.Aereo.SeguroPassageiro&gt;()
           .ToTable("SegurosPassageiros");
  
           //Tried both lines below (together and separeted) with no success:            
          //modelBuilder.Entity&lt;Dominio.Aereo.SeguroPassageiro&gt;().HasRequired(s =&gt; s.Passageiro).WithOptional();
          //modelBuilder.Entity&lt;Dominio.Aereo.Passageiro&gt;().HasMany(p =&gt; p.Seguros).WithRequired();
  
          //Tried "linking" the column "CodPassageiro" on table "SegurosPassageiros" to                   
          //column "CodLocPassageiroInterno" on table "LocsPassageiros". No success.
          modelBuilder.Entity&lt;Dominio.Aereo.SeguroPassageiro&gt;().Property(s =&gt; s.CodLocPassageiroInterno).HasColumnName("CodPassageiro");
      }
  </code></pre>
  
  <p>With this model, after dozens of trials, the closest I could reach was getting a List in the Passageiro object but with the wrong association: 
   "SegurosPassageiros.CodPassageiro" and "LocsPassageiros.CodLocPassageiro" (instead of "LocsPassageiros.CodLocPassageiro*<strong>Interno*</strong> "). 
  EF insists on getting the wrong association (getting the PK on LocsPassageiros).</p>
  
  <p>Anyone know how can I get this association wrking in EF?</p>