I have a DB that was built with EF 4 code first, here’s two models:
Public Class HvacAudit
Public Property HvacAuditID As Integer
Public Overridable Property HvacAuditPictures As IEnumerable(Of HvacAuditPicture)
... other properties
End Class
Public Class HvacAuditPicture
Public Property HvacAuditPictureID As Integer
Public Property HvacAuditID As Integer
Public Overridable Property HvacAudit As HvacAudit
... other properties
End Class
When I pull my HvacAudit records from the db, they don’t have HvacAuditPictures! Even though if I pull the pictures directly, they are there. Consider
context.HvacAudits.Where(Function(hva) hva.HvacAuditID = 1).HvacAuditPictures Is Nothing 'evaluates true
context.HvacAuditPictures.Where(Function(p) p.HvacAuditID = 1).Count() 'is currently 20
Here’s my context class:
Public Class EfContext
Inherits DbContext
Implements IDataSource
Public Property HvacAudits As IDbSet(Of HvacAudit) Implements IDataSource.HvacAudits
Public Property Vendors As IDbSet(Of Vendor) Implements IDataSource.Vendors
Public Property HvacAuditPictures As IDbSet(Of HvacAuditPicture) Implements IDataSource.HvacAuditPictures
Public Property Sites As IDbSet(Of Site)
Public Property Markets As IDbSet(Of Market)
Public Sub SaveChanges1() Implements Domain.DataSource.IDataSource.SaveChanges
SaveChanges()
End Sub
End Class
Found the answer. The navigation property for the “many” side of a relationship has to be an ICollection(Of T), not an IEnumerable(Of T).