2016-04-09 43 views
1

我使用LINQ to实体从表中检索记录,并从其他表中检索相关记录。从Linq到实体获取表和相关相关表的记录

这里是我的表有关系:

enter image description here

这里是我的LINQ到实体来检索传感器测量和相关表格警报描述记录。

public IEnumerable<SensorsMeasure> GetSensorsMeasureWithAlertDescription() 
{ 
    return SensorObservationEntities.SensorsMeasures.Include(d => d.AlertsDescription.AlertDescription).ToList(); 
} 

但在上面的查询我得到这个错误:

A specified Include path is not valid. The EntityType 'SensorObservationModel.AlertsDescription' does not declare a navigation property with the name 'AlertDescription'. 

任何想法,为什么我得到上述错误,以及如何解决它?

回答

2

这是因为AlertDescription不是导航 AlertsDescription类型的属性 - 它只是常规属性,所以你不需要包含它。只要这样做:

public IEnumerable<SensorsMeasure> GetSensorsMeasureWithAlertDescription() 
{ 
    return SensorObservationEntities.SensorsMeasures.Include(d => d.AlertsDescription).ToList(); 
} 
+0

以及如何从AlertsDescription表中获取警报描述属性? – Michael

+0

只需访问SensorsMeasure对象的AlertsDescription.AlertDescription属性即可。因为您使用Include加载了AlertsDescription - 您可以访问它的所有属性(当然假设SensorsMeasure首先关联了AlertsDescription - 否则该属性将为空) – Evk

+0

您似乎有点困惑,认为Include与某些常规属性有关,但事实并非如此 - 无论如何,每个EF实体都将具有所有常规属性,并始终加载它们。您只能通过Include来控制导航属性是否会被加载。 – Evk