c# - LINQ with EF6: Losing second reference in two column table -
c# - LINQ with EF6: Losing second reference in two column table -
i have unusual problem here.
in database, have table called "neighbourcountry": consists of source country, , associated neighbour country.
public class neighbourcountry { [key] public int neighbourcountryid { get; set; } public country source { get; set; } public country neighbour { get; set; } } it filled pattern:
source_a,neighbour_x source_a,neighbour_u source_a,neighbour_t source_b,neighbour_n source b,neighbour_p now wanted neighbours of 1 country simple method: (for easier debugging, seek first neighbour of country)
private string getneighbours(applicationdbcontext context, country paracountry) { var neighbours = context.neighbourcountries.where(b => b.source.countryid == paracountry.countryid).firstordefault(); homecoming ""; } and now, when hover on "neighbours", this:
as can see, find source country, says there no neighbour it. not correct, database filled data:
the source country in case country id 1. in database, country id 1 listed correctly neighbours.
now comes stranger part: when seek other way round, linqing neighbour straight instead source, same scenario, other way round.
var neighbours = context.neighbourcountries.where(b => b.neighbour.countryid == paracountry.countryid).firstordefault(); as said, this:
so linq find neighbour, can't find first associated source country. seems can't connect source country , neighbour country.
do have thought problem?
thank much!
just problem of lazy loading say.
if want retrieve neighbour , 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, don't need include related entity in clause, makes maybe things clearer).
c# asp.net-mvc linq entity-framework ef-code-first
Comments
Post a Comment