title: Entity Framework Code first foreign key mapping
body: <p>I have 2 entities:</p>
  
  <ul>
  <li>Claim (ID, other primirives, one more relation) - Claims table</li>
  <li>ClaimDetail (ID, ClaimID, other primitives, relations) - ClaimDetails
  table</li>
  </ul>
  
  <p>Mapping:</p>
  
  <pre><code>modelBuilder.Entity&lt;Claim&gt;()
                          .HasMany(x =&gt; x.ClaimDetails)
                          .WithRequired(x =&gt; x.Claim)
                          .HasForeignKey(x =&gt; x.ClaimID)
  </code></pre>
  
  <p>A generated query:</p>
  
  <pre><code>exec sp_executesql N'SELECT 
  [Extent1].[ID] AS [ID], 
  [Extent1].[ClaimID] AS [ClaimID], 
  [Extent1].[UserID] AS [UserID], 
  [Extent1].[ClaimTypeID] AS [ClaimTypeID], 
  [Extent1].[PermissionID] AS [PermissionID], 
  [Extent1].[StartDate] AS [StartDate], 
  [Extent1].[EndDate] AS [EndDate], 
  [Extent1].[Justification] AS [Justification], 
  [Extent1].[Claim_ID] AS [Claim_ID]
  FROM  [dbo].[ClaimDetails] AS [Extent1]
  OUTER APPLY  (SELECT TOP (1) [Project1].[ClaimDetailStatusID] AS [ClaimDetailStatusID]
      FROM ( SELECT 
          [Extent2].[ClaimDetailStatusID] AS [ClaimDetailStatusID], 
          [Extent2].[CreatedOn] AS [CreatedOn]
          FROM [dbo].[ClaimDetailStatus] AS [Extent2]
          WHERE [Extent1].[ID] = [Extent2].[ClaimDetailID]
      )  AS [Project1]
      ORDER BY [Project1].[CreatedOn] DESC ) AS [Limit1]
  WHERE ([Extent1].[UserID] = @p__linq__0) AND ( NOT ([Limit1].[ClaimDetailStatusID] IN (12,11,4,7,13,10)))',N'@p__linq__0 int',@p__linq__0=4106
  </code></pre>
  
  <p>If i left out the mapping then EF will generate another column called Claim_ID1. What is wrong with this? How should I define the relation?</p>
  
  <p>EDIT</p>
  
  <pre><code>public class Claim
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int ID { get; set; }
      [Required]
      public int SenderID { get; set; }
  
      [NotMapped]
      public UserModel Sender { get; set; } //TODO:
  
      [Required]
      public DateTime Created { get; set; }
  
      public string Note { get; set; }
      [Required]
      public int BossID { get; set; }
  
      [NotMapped]
      public UserModel Boss { get; set; } //TODO:
  
      public virtual IList&lt;ClaimDetail&gt; ClaimDetails { get; set; }
      public virtual IList&lt;UserClaim&gt; UserClaims { get; set; }
  }
  
  public class BaseClaimItem
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int ID { get; set; }
  
      [Required]
      public int PermissionID { get; set; }
  
      [Required]
      public DateTime StartDate { get; set; }
  
      [Required]
      public DateTime EndDate { get; set; }
  
      public string Justification { get; set; }
  
      public virtual Permission Permission { get; set; }
  
      [NotMapped]
      public virtual UserModel Claimant { get; set; }
  }
  
  
  public class ClaimDetail : BaseClaimItem
  {
      [Required]
      public virtual int ClaimID { get; set; }
  
      [Required]
      public int UserID {get;set;}
  
      [NotMapped]
      public override UserModel Claimant {get;set;}
  
      [Required, Range(1, 5)]
      public int ClaimTypeID { get; set; }
  
      public virtual Claim Claim { get; set; }
      public virtual IList&lt;ClaimDetailStatus&gt; ClaimDetailStatus { get; set; }
      public virtual IList&lt;Task&gt; Tasks { get; set; }
      public virtual IList&lt;ClaimDetailApprovalStep&gt; ClaimDetailApprovalSteps { get; set; }
  
      public virtual ClaimType ClaimType { get; set; }
  }
  </code></pre>