LINQ with EF6:在两列表中丢失第二个引用(LINQ with EF6: Losing second reference in two column table)

编程入门 行业动态 更新时间:2024-10-05 21:15:42
LINQ with EF6:在两列表中丢失第二个引用(LINQ with EF6: Losing second reference in two column table)

我这里有一个奇怪的问题。

在我的数据库中,我有一个名为“NeighbourCountry”的表:它包含一个源国家和一个相关的邻国。

public class NeighbourCountry { [Key] public int NeighbourCountryID { get; set; } public Country Source { get; set; } public Country Neighbour { get; set; } }

它像这样的模式填充:

source_a,neighbour_x source_a,neighbour_u source_a,neighbour_t source_b,neighbour_n source b,neighbour_p

现在我只想用一个简单的方法来获得一个国家的邻居:(为了便于调试,我只想尝试一个国家的第一个邻居)

private string GetNeighbours(ApplicationDbContext context, Country paracountry) { var neighbours = context.NeighbourCountries.Where(b => b.Source.CountryID == paracountry.CountryID).FirstOrDefault(); return ""; }

现在,当我将鼠标悬停在“邻居”身上时,我得到了这个:

正如你所看到的,它确实找到了源国,但是说它没有邻居。 这是不正确的,数据库充满了这些数据:

在这种情况下,源国家/地区是ID为1的国家/地区。在数据库中,ID为1的国家/地区与其邻居正确列出。

现在来到更奇怪的部分:当我以相反的方式尝试它时,直接LINQing到邻居而不是源,我得到相同的场景,反过来。

var neighbours = context.NeighbourCountries.Where(b => b.Neighbour.CountryID == paracountry.CountryID).FirstOrDefault();

正如我所说,然后我得到了这个:

所以Linq找到了这个邻居,但现在它找不到第一个相关的源国。 我好像无法连接源国和邻国。

你知道这可能是什么问题吗?

非常感谢!

I have a strange problem here.

In my database, I have a table called "NeighbourCountry": It consists of a source country, and an associated neighbour country.

public class NeighbourCountry { [Key] public int NeighbourCountryID { get; set; } public Country Source { get; set; } public Country Neighbour { get; set; } }

It is filled like this pattern:

source_a,neighbour_x source_a,neighbour_u source_a,neighbour_t source_b,neighbour_n source b,neighbour_p

Now I just wanted to get the neighbours of one country with a simple method: (For easier debugging, I just try to get only the FIRST neighbour of a country)

private string GetNeighbours(ApplicationDbContext context, Country paracountry) { var neighbours = context.NeighbourCountries.Where(b => b.Source.CountryID == paracountry.CountryID).FirstOrDefault(); return ""; }

And now, when I hover over "neighbours", I get this:

As you can see, it does find the source country, but says there is no neighbour to it. That is not correct, the database is filled with this data:

The source country in this case is the country with the ID 1. In the database, the country with ID 1 is listed correctly with its neighbours.

Now comes the even stranger part: When I try it the other way round, LINQing to the neighbour directly instead to the source, I get the same scenario, just the other way round.

var neighbours = context.NeighbourCountries.Where(b => b.Neighbour.CountryID == paracountry.CountryID).FirstOrDefault();

As I said, then I get this:

So Linq DOES find this neighbour, but now it can't find the first associated source country. I seems like it can't connect source country and neighbour country.

Do you have any idea what could be the problem?

Thank you so much!

最满意答案

我想说的只是延迟加载的问题。

如果要检索邻居和源,请使用Include急切加载它们

context.NeighbourCountries .Include(m => m.Neighbour) .Include(m => m.Source) .Where(b => b.Neighbour.CountryID == paracountry.CountryID).FirstOrDefault();

(实际上,您不需要包含where子句中的相关实体,但这可能使事情更清楚)。

Just a problem of lazy loading I would say.

If you want to retrieve Neighbour and Source, eager load them, using Include

context.NeighbourCountries .Include(m => m.Neighbour) .Include(m => m.Source) .Where(b => b.Neighbour.CountryID == paracountry.CountryID).FirstOrDefault();

(In fact, you don't need to include the related entity which is in the where clause, but that makes maybe things clearer).

更多推荐

本文发布于:2023-07-09 10:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二个   列表中   LINQ   table   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!