为什么我的模型的相关数据没有加载?

时间:2012-07-17 14:28:14

标签: .net entity-framework-4 ef-code-first

我有一个首先使用EF 4代码构建的数据库,这里有两个模型:

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

当我从数据库中提取HvacAudit记录时,他们没有HvacAuditPictures!即使我直接拉动图片,它们也在那里。考虑

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

这是我的上下文类:

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

1 个答案:

答案 0 :(得分:0)

找到答案。关系“很多”方面的导航属性必须是 ICollection(Of T),而不是 IEnumerable(Of T)

Public Class HvacAudit
    Public Property HvacAuditID As Integer

    Public Overridable Property HvacAuditPictures As ICollection(Of HvacAuditPicture)

   ... other properties
End Class