6 ms·
My VB is a bit rusty, but you will need to define associations in the configuration. First add foreign keys to to models (e.g. In the Build class add MainlineI
by Xdes 12y ago
My VB is a bit rusty, but you will need to define associations in the configuration.
First add foreign keys to to models (e.g. In the Build class add MainlineID for the Mainline object).
Then you can define the configuration:
Public Class BuildConfiguration
Implements EntityTypeConfiguration(Of Build)
Public Sub New()
ToTable("Build")
' Other mappings
' Define one-to-many
HasRequired(Function(x) x.Mainline) _
.WithMany(Function(x) x.Builds) _
.HasForeignKey(Function(x) x.MainlineID)
End Sub
End Class
Inside your context you load the configuration:
Public Class MyContext
Inherits DbContext
Public Property Builds As DbSet(Of Build)
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Configurations.Add(New BuildConfiguration())
End Sub
End Class
Then you can use the Include extension method (or eager loading) on the DbContext to populate the object.
Using context As New MyContext()
Return context.Builder.Include(Function(b) b.Mainline)
End Using
See also:
http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx http://weblogs.asp.net/manavi/archive/2011/05/17/association...
- TheRealDunkirk 12y agoI had seen that linked writeup before, but was confused, because much of that code looked unnecessary for EF6. I relegated it to only being applicable to version 4. Indeed, all I needed to do (as below) was use .SelectMany(). That being said, though... Wow, your Fluent API-fu is strong.