I'm trying to set the foreign key name (not the foreign key column) using Code First Migrations on EF6.4.
I know that it can be set by updating the generated migration code, like so:
.ForeignKey("Documents", Function(t) t.DocumentId, cascadeDelete:=True, name:="FK_Sections_Documents")
...but I'd like to do it before the migration is added, using the Fluent API.
I seem to recall something about the HasForeignKey()
call accepting a Func
that contains a call to an anonymous type in its body, such as what we find here. But I'll be darned if I can locate anything discussing what the general structure of that type should be.
The official documentation doesn't discuss it:
Nor do these similar Q&As quite exactly address the issue:
- EF Code First Fluent API specifying the Foreign Key property
- Entity Framework Code First Mapping Foreign Key Using Fluent API
- Specifying Foreign Key Entity Framework Code First, Fluent Api
This same question was asked a couple of months ago here, but so far it hasn't received an answer.
I'm using EntityTypeConfiguration(Of T)
. Here's my 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
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?
--UPDATE--
I tried this:
Me.HasRequired(Function(Section) Section.Document).WithMany.HasForeignKey(Function(Section) New With {.DependentKeyExpression = Section.DocumentId, .Name = "FK_Sections_Documents"})
...but a migration creation attempt answered with this:
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 }'.
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.
The question still stands: How may we specify the foreign key name using the Fluent API in EF6.4?