使用 Linq to entity 从 table 和相关 table 获取记录

Get records from table and related related table using Linq to entity

我使用 LINQ to entity 从 table 中检索记录,并从另一个 table 中检索相关记录。

这是我的 table 关系:

这里是我的 LINQ to 实体,用于从传感器测量和相关 table 警报描述中检索记录。

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 不是 navigation 属性 类型的 AlertsDescription - 它只是常规的 属性,因此您不需要包含它。只是做:

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