使用Linq到实体从表和相关的相关表中获取记录

问题描述:

我使用LINQ进行实体检索以从表中检索记录,并从另一个表中检索相关记录.

I use LINQ to entity to retrive records from table and there related records from another table.

这是与我有关的表:

这是我的LINQ to实体,用于从Sensor Measure和相关的表警报描述中检索记录.

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?

这是因为AlertDescription不是AlertsDescription类型的 navigation 属性-它只是常规属性,因此您无需包含它.只要做:

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();
}