title: Creating composite primary key consisting off foreign keys in fluent API with ASP.NET core
body: <p>The question here is how can i generate a composite primary key consisting of two foreign keys? </p>
  
  <p>I have tried: </p>
  
  <pre><code>public class ActiveQuestions_Questions 
  {
      [Column(Order = 0), Key, ForeignKey("ActiveQuestion")]
      public string ActiveQuestionId {get; set;}
  
      [Column(Order = 1), Key, ForeignKey("Question")]
      public string QuestionId {get; set; }
  }
  </code></pre>
  
  <p>Which gives me: Entity type 'ActiveQuestions_Questions' has composite primary key defined with data annotations. To set composite primary key, use fluent API.</p>
  
  <p>Then i tried using the fluent api in the model builder without the annotations. </p>
  
  <pre><code>builder.Entity&lt;ActiveQuestions_Questions&gt;(
              build =&gt; 
              {
                  build.HasKey(t =&gt; new {t.ActiveQuestionId, t.QuestionId}); 
                  build.HasOne(t =&gt; t.QuestionId).WithOne().HasForeignKey&lt;Question&gt;(qe =&gt; qe.QuestionId);
                  build.HasOne(t =&gt; t.ActiveQuestionId).WithOne().HasForeignKey&lt;ActiveQuestion&gt;(qe =&gt; qe.ActivateQuestionId); 
              }
          );    
  </code></pre>
  
  <p>Which gave me : 
  The navigation property 'QuestionId' cannot be added to the entity type 'ActiveQuestions_Questions' because a property with the same name already exists on entity type 'ActiveQuestions_Questions'.</p>
  
  <p>Anyone that can point me in the right direction? </p>
  
  <p>The Questions class </p>
  
  <pre><code>public class Question 
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public string QuestionId {get; set;}
  
      [Required]
      public string Text {get; set;}
  }
  </code></pre>
  
  <p>The ActiveQuestion class: </p>
  
  <pre><code>public class ActiveQuestion 
  {
      private DateTime _lastUpdated = DateTime.Now; 
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public string ActivateQuestionId {get; set;}
  
      public DateTime LastUpdated 
      {
          get 
          {
              return this._lastUpdated; 
          }
          set 
          {
              this._lastUpdated = value;
          }
      }
  }
  </code></pre>