title: Composite Key EF Core getting error when using Fluent Api
body: <p>So I have the following class in Entity Framework Core. I am trying to do a code first migration and can't for the life of me figure out how to make the fluent API for this work. </p>
  
  <pre><code>public class Participants
  {
      public Activity Activity { get; set; } //Class with Id and Name of Activity
      public ApplicationUser Participant { get; set; } 
  
      [Key]
      [Column(Order = 1)]
      public int ActivityId { get; set; }
  
  
      [Key]
      [Column(Order = 2)]
      public string ParticipantId { get; set; }
  }
  </code></pre>
  
  <p>In EF6 I was able to do this in OnModelCreating to get it to work fine. </p>
  
  <pre><code> modelBuilder.Entity&lt;Attendance&gt;()
              .HasRequired(a =&gt; a.Activity)
              .WithMany()
              .WillCascadeOnDelete(false);
  </code></pre>
  
  <p>But in EF Core I get </p>
  
  <blockquote>
    <p>" Entity type 'Participants' has composite primary key defined with data annotations. To set composite primary key, use fluent API."</p>
  </blockquote>
  
  <p>I have tried using </p>
  
  <pre><code>modelBuilder.Entity&lt;Participants&gt;().HasKey(p =&gt; new {p.Activity, p.Participant});
  </code></pre>
  
  <p>But, that just leads to </p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint 'FK_Participants_AspNetUsers_ParticipantId' on table 'Participants' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.</p>
  </blockquote>
  
  <p>If there is a better way to do the whole thing I'm open to suggestions. If you have pluralsight subscription, I'm basically trying to get "Become a Full Stack Developer" by Mosh Hamedani to work in EF core. The example is in "13-full-stack-fundamentals" folder.  </p>
  
  <p>UPDATE: Also tried</p>
  
  <pre><code>        modelBuilder.Entity&lt;Participants&gt;()
              .HasOne(p =&gt; p.Activity)
              .WithMany()
              .OnDelete(DeleteBehavior.Cascade);
  </code></pre>
  
  <p>Still got </p>
  
  <blockquote>
    <p>"Entity type 'Participants' has composite primary key defined with data annotations. To set composite primary key, use fluent API."</p>
  </blockquote>
  
  <p>UPDATE 2: After trying Roy's suggestion this is what I'm getting</p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint 'FK_Participants_AspNetUsers_ParticipantId' on table 'Participants' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.</p>
  </blockquote>
  
  <p>UPDATE 3: In the Migration </p>
  
  <p>I removed one of the OneDelete: ReferntialAction.Cascade and it worked. I removed the one off of FK_Participants_AspNetUsers_ParticipantId.</p>
  
  <p>I also changed to this in my OnModelCreating</p>
  
  <pre><code>        modelBuilder.Entity&lt;Participants&gt;()
              .HasKey(p =&gt; new { p.ActivityId, p.ParticipantId });
          base.OnModelCreating(modelBuilder);
  
          //Added this ( Not sure if it's needed if anyone knows let me know) 
          modelBuilder.Entity&lt;Participants&gt;()
              .HasOne(p =&gt; p.Activity)
              .WithMany()
              .OnDelete(DeleteBehavior.Cascade);
  </code></pre>