使用Linq to entity从表和相关联的表获取记录[英] Get records from table and related related table using Linq to entity

本文是小编为大家收集整理的关于使用Linq to entity从表和相关联的表获取记录的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

本文地址:https://www.itbaoku.cn/post/1557089.html

问题描述

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

Here is my tables with relation:

enter image description here

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