title: EF5 Code First Fluent EntityTypeConfiguration for tracks & albums models
body: <p>I am modeling music tracks and albums where Albums have many tracks and Tracks can be on only one album, with a join table to specify it's position in the album listing.</p>
  
  <p>Here are my models:</p>
  
  <pre><code>public class Track
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public int AlbumTrackId { get; set; }
      public virtual AlbumTrack AlbumTrack { get; set; }
  }
  
  public class Album
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;AlbumTrack&gt; AlbumTracks { get; set; }
  }
  
  public class AlbumTrack
  {
      public int AlbumId { get; set; }
      public virtual Album Album { get; set; }
  
      public int TrackId { get; set; }
      public virtual Track Track { get; set; }
  
      public int Position { get; set; }
  }
  </code></pre>
  
  <p>and my EntityTypeConfiguration</p>
  
  <pre><code>public class AlbumTrackConfiguration : EntityTypeConfiguration&lt;AlbumTrack&gt;
  {
  
      public AlbumTrackConfiguration()
      {
          // AlbumTrack has a composite key
          HasKey(at =&gt; new {at.AlbumId, at.TrackId});
  
          // AlbumTrack has one Album, Albums have many AlbumTracks
          HasRequired(at =&gt; at.Album)
              .WithMany(a =&gt; a.AlbumTracks)
              .HasForeignKey(at =&gt; at.AlbumId)
              .WillCascadeOnDelete(true);
  
          // AlbumTrack has one Track, Tracks have one AlbumTrack
          HasRequired(at =&gt; at.Track)
              .WithRequiredPrincipal(t=&gt;t.AlbumTrack)
              .WillCascadeOnDelete(true);
      }
  
  }
  </code></pre>
  
  <p>The one to many relationship between Albums and AlbumTracks is fine, but in addition to the expected</p>
  
  <pre><code>[AlbumTrackId] [int] NOT NULL,
  </code></pre>
  
  <p>Code First keeps adding</p>
  
  <pre><code>[AlbumTrack_AlbumId] [int] NOT NULL,
  [AlbumTrack_TrackId] [int] NOT NULL
  </code></pre>
  
  <p>to the Tracks table.</p>
  
  <p>How can I model the Track to Album Track relationship better so that only the properties I specify translate in db fields?</p>
  
  <p>[Yes, in my world a track can only exist on a single album!]</p>
  
  <p>thanks.</p>