title: How can I set the foreign key name using the Fluent API with Code First Migrations?
body: <p>I'm trying to set the foreign key name (not the foreign key column) using Code First Migrations on EF6.4.</p>
  
  <p>I know that it can be set by updating the generated migration code, like so:</p>
  
  <pre><code>.ForeignKey("Documents", Function(t) t.DocumentId, cascadeDelete:=True, name:="FK_Sections_Documents")
  </code></pre>
  
  <p>...but I'd like to do it before the migration is added, using the Fluent API.</p>
  
  <p>I seem to recall something about the <code>HasForeignKey()</code> call accepting a <code>Func</code> that contains a call to an anonymous type in its body, such as what we find <a href="https://stackoverflow.com/q/50740883">here</a>. But I'll be darned if I can locate anything discussing what the general structure of that type should be.</p>
  
  <p>The official documentation doesn't discuss it:</p>
  
  <ul>
  <li><a href="https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/vb" rel="nofollow noreferrer">Fluent API with VB.NET</a></li>
  <li><a href="https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.modelconfiguration.configuration.dependentnavigationpropertyconfiguration-1.hasforeignkey" rel="nofollow noreferrer">HasForeignKey(Of TKey)</a></li>
  </ul>
  
  <p>Nor do these similar Q&amp;As quite exactly address the issue:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/19359608">EF Code First Fluent API specifying the Foreign Key property</a></li>
  <li><a href="https://stackoverflow.com/q/5489569">Entity Framework Code First Mapping Foreign Key Using Fluent API</a></li>
  <li><a href="https://stackoverflow.com/q/9858897">Specifying Foreign Key Entity Framework Code First, Fluent Api</a></li>
  </ul>
  
  <p>This same question was asked a couple of months ago <a href="https://stackoverflow.com/q/58207851">here</a>, but so far it hasn't received an answer.</p>
  
  <p>I'm using <code>EntityTypeConfiguration(Of T)</code>. Here's my code:</p>
  
  <pre><code>Namespace Configuration
    Friend Class SectionConfig
      Inherits EntityTypeConfiguration(Of Db.Section)
  
      Public Sub New()
        Me.HasRequired(Function(Section) Section.Document).WithMany.HasForeignKey(Function(Section) Section.DocumentId)
  
        Me.Property(Function(Section) Section.DocumentId).IsRequired()
        Me.Property(Function(Section) Section.SectionId).IsRequired()
        Me.Property(Function(Section) Section.IsSent).IsRequired()
        Me.Property(Function(Section) Section.Markup).IsRequired.IsMaxLength()
        Me.Property(Function(Section) Section.Title).IsRequired.HasMaxLength(60)
  
        Me.HasIndex(Function(Section) Section.DocumentId).HasName("IX_Sections_DocumentId")
        Me.HasIndex(Function(Section) Section.SectionId).HasName("IX_Sections_SectionId")
        Me.HasIndex(Function(Section) Section.Title).HasName("IX_Sections_Title")
  
        Me.Ignore(Function(Section) Section.Subject)
      End Sub
    End Class
  End Namespace
  </code></pre>
  
  <p>How does one set a foreign key name, or—even more specific, assuming I'm remembering correctly—what should be the general structure of that anonymous type?</p>
  
  <p><strong>--UPDATE--</strong></p>
  
  <p>I tried this:</p>
  
  <pre><code>Me.HasRequired(Function(Section) Section.Document).WithMany.HasForeignKey(Function(Section) New With {.DependentKeyExpression = Section.DocumentId, .Name = "FK_Sections_Documents"})
  </code></pre>
  
  <p>...but a migration creation attempt answered with this:</p>
  
  <blockquote>
    <p>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The properties expression 'Section => new VB$AnonymousType_0`2(DependentKeyExpression = Section.DocumentId, Name = "FK_Sections_Documents")' is not valid. The expression should represent a property: C#: 't => t.MyProperty'  VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }'  VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.</p>
  </blockquote>
  
  <p>So, given that the anonymous type construct is for specifying the key column(s), it's not the way to specify a foreign key name.</p>
  
  <p>The question still stands: How may we specify the foreign key name using the Fluent API in EF6.4?</p>