我这里有一个奇怪的问题。
在我的数据库中,我有一个名为“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_pNow 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).
更多推荐
发布评论