问题描述
我使用 LINQ to entity 从表中检索记录,并从另一个表中检索相关记录.
这是我的关系表:
这是我的 LINQ to entity,用于从 Sensors Measure 和相关表警报描述中检索记录.
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'.
知道为什么会出现上述错误以及如何解决它吗?
推荐答案
这是因为 AlertDescription 不是 AlertsDescription 类型的 navigation 属性 - 它只是常规属性,因此您不需要包含它.做吧:
public IEnumerable<SensorsMeasure> GetSensorsMeasureWithAlertDescription() { return SensorObservationEntities.SensorsMeasures.Include(d => d.AlertsDescription).ToList(); }
问题描述
I use LINQ to entity to retrive records from table and there related records from another table.
Here is my tables with relation:
And here is my LINQ to entity to retrieve records from Sensors Measure and related table alert description.
public IEnumerable<SensorsMeasure> GetSensorsMeasureWithAlertDescription() { return SensorObservationEntities.SensorsMeasures.Include(d => d.AlertsDescription.AlertDescription).ToList(); }
But on the query above I get this error:
A specified Include path is not valid. The EntityType 'SensorObservationModel.AlertsDescription' does not declare a navigation property with the name 'AlertDescription'.
Any idea why I get the error above and how to fix it?
推荐答案
That's because AlertDescription is not navigation property of AlertsDescription type - it's just regular property, so you don't need to include it. Just do:
public IEnumerable<SensorsMeasure> GetSensorsMeasureWithAlertDescription() { return SensorObservationEntities.SensorsMeasures.Include(d => d.AlertsDescription).ToList(); }