Asp.net mvc 使用实体框架选择子实体的父实体

Asp.net mvc 使用实体框架选择子实体的父实体,asp.net-mvc,vb.net,entity-framework,Asp.net Mvc,Vb.net,Entity Framework,我在每个父实体和它们的子实体之间都有一对多的关系。当我执行.Where语句来选择某些子元素时,如何同时选择父实体 父对象: Class Parent Property Id As Integer Property ParentName As String Overridable Property Vehicles As ICollection(Of Vehicle) End Class 子对象: Class Vehicle Property Id As Inte

我在每个父实体和它们的子实体之间都有一对多的关系。当我执行.Where语句来选择某些子元素时,如何同时选择父实体

父对象:

Class Parent
    Property Id As Integer
    Property ParentName As String
    Overridable Property Vehicles As ICollection(Of Vehicle)
End Class
子对象:

Class Vehicle
    Property Id As Integer
    Property VehicleRegistration As String
End Class
我使用以下语句选择所需的子实体:

DbContext.Vehicles.Where(Function(m) m.VehicleRegistration.Contains(searchTerm)).ToList()

我需要能够遍历此列表并选择每个子级的父级,或者在输出中包含父级列,以便在MVC视图中显示。

Vehicle
需要一个属性
ParentID
(我假设您的数据库中必须有该属性才能创建关系)是的,脚手架创建了带有Parent_Id字段的数据库表。我可以显式地选择父实体,然后使用parent.fine遍历子实体,这样就定义了关系。