title: How to set ForeignKey and Index properties using IEntityTypeConfiguration in EF Core 2.0
body: <p>I am attempting to use the <em>EntityConfiguration</em> interface which is new in EF Core 2.0. </p>
  
  <p><strong>Addendum</strong> Probably helpful to see the Message class</p>
  
  <pre><code>public class Message 
  {
      public int MessageId { get; set; }
  
      [Required]
      public string MessageForUserId { get; set; }
      public virtual ApplicationUser MessageForUser { get; set; }
  
      [Required]
      public string MessageFromUserId { get; set; }
  
      public virtual ApplicationUser MessageFromUser { get; set; }
  
      [Required]
      public string MessageContent { get; set; }
  
      public bool MessageRead { get; set; }
  
      [Required]
      public DateTime MessageCreatedOn { get; set; }
  
      public DateTime? MessageReadOn { get; set; }
  }
  </code></pre>
  
  <p>and the AppUser class</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public DateTime DateOfBirth { get; set; }
      public Gender Gender { get; set; }
      public SubscriptionLevel SubscriptionLevel { get; set; }
      public NotificationType NotificationType { get; set; }
      public string AvatarUrl { get; set; }
      public ICollection&lt;Message&gt; UserMessages { get; set; }
      public ICollection&lt;Message&gt; SentMessages { get; set; }
      public ICollection&lt;IdentityUserClaim&lt;string&gt;&gt; Claims { get;  set; }
  }
  </code></pre>
  
  <p>Here is how the entity is defined in my <code>OnModelCreating</code> method.</p>
  
  <pre><code>builder.Entity&lt;Message&gt;()
              .HasOne(x =&gt; x.MessageForUser)
              .WithMany(x =&gt; x.UserMessages)
              .HasForeignKey(x =&gt; x.MessageForUserId)
              .OnDelete(DeleteBehavior.Restrict);
  
  builder.Entity&lt;Message&gt;()
              .HasOne(x =&gt; x.MessageFromUser)
              .WithMany(x =&gt; x.SentMessages)
              .HasForeignKey(x =&gt; x.MessageFromUserId)
              .OnDelete(DeleteBehavior.Cascade);
  </code></pre>
  
  <p>My IEntityTypeConfiguration</p>
  
  <pre><code>public class AppUserConfig : IEntityTypeConfiguration&lt;ApplicationUser&gt;
  {
      public void Configure(EntityTypeBuilder&lt;ApplicationUser&gt; builder)
      {           
          builder.Property(u =&gt; u.FirstName).HasMaxLength(31);
          builder.Property(u =&gt; u.FirstName).IsRequired();
  
          builder.Property(u =&gt; u.LastName).HasMaxLength(55);
          builder.Property(u =&gt; u.LastName).IsRequired();
  
          builder.Property(u =&gt; u.DateOfBirth).HasColumnType("datetime");
          builder.Property(u =&gt; u.DateOfBirth).IsRequired();
  
          builder.Property(u =&gt; u.Gender).IsRequired();
      }
  }
  </code></pre>
  
  <p>The EF 6 way had a <code>HasForeignKey</code> method off builder which is no longer available and I cant seem to find any way to add them.</p>
  
  <p>Here is how the migration built the table from my <code>OnModelCreating</code> method. I'd like to put most of these items in my configuration class.</p>
  
  <pre><code>migrationBuilder.CreateIndex(
              name: "IX_Messages_MessageForUserId",
              schema: "AppContext",
              table: "Messages",
              column: "MessageForUserId");
  
  migrationBuilder.CreateIndex(
              name: "IX_Messages_MessageFromUserId",
              schema: "AppContext",
              table: "Messages",
              column: "MessageFromUserId");
  
  migrationBuilder.AddForeignKey(
              name: "FK_Messages_AspNetUsers_MessageForUserId",
              schema: "AppContext",
              table: "Messages",
              column: "MessageForUserId",
              principalSchema: "AppContext",
              principalTable: "AspNetUsers",
              principalColumn: "Id",
              onDelete: ReferentialAction.Restrict);
  
  migrationBuilder.AddForeignKey(
              name: "FK_Messages_AspNetUsers_MessageFromUserId",
              schema: "AppContext",
              table: "Messages",
              column: "MessageFromUserId",
              principalSchema: "AppContext",
              principalTable: "AspNetUsers",
              principalColumn: "Id",
              onDelete: ReferentialAction.Cascade);
  </code></pre>
  
  <p>There still is a <code>HasIndex</code> method, but I'm not sure how to duplicate what the migration is producing in the lambda expression for <code>HasIndex</code>.</p>