Entity Framework - eagerly loading two many-to-many relationships

Sorry for this for so long, but at least I think I have all the information to understand and maybe help?

I would like to load data from my database using downloadable download.

Data is tuned in five tables, setting two levels of m: n relationships. Thus, there are three tables containing data (ordered by hierarchy from top to bottom):

CREATE TABLE [dbo].[relations](
    [relation_id] [bigint] NOT NULL
)

CREATE TABLE [dbo].[ways](
    [way_id] [bigint] NOT NULL
)

CREATE TABLE [dbo].[nodes](
    [node_id] [bigint] NOT NULL,
    [latitude] [int] NOT NULL,
    [longitude] [int] NOT NULL
)

The first two really consist only of their own identifier (for binding other data that does not matter here).

Between these three data tables are two m: n tables with a sort prompt:

CREATE TABLE [dbo].[relations_ways](
    [relation_id] [bigint] NOT NULL,
    [way_id] [bigint] NOT NULL,
    [sequence_id] [smallint] NOT NULL
)

CREATE TABLE [dbo].[ways_nodes](
    [way_id] [bigint] NOT NULL,
    [node_id] [bigint] NOT NULL,
    [sequence_id] [smallint] NOT NULL
)

, , OpenStreetMap. Entity Framework , . m: n . ( , EF m: n - ?)



: - .

, m: n, . .

IQueryable<relation> query = context.relations;
query = query.Where( ... ); // filters down to exactly one
query = query.Include(r => r.relation_members);
relation rel = query.SingleOrDefault();

1: n info - , . , 1: n, "".

, :

query = query.Include(r => r.relation_members.Select(rm => rm.way));

, , ?

, , node . :

foreach (relation_member rm in rel.relation_members) {
    IQueryable<way_node> query = rm.way.way_nodes.AsQueryable();
    query = query.Include(wn => wn.node);
    query.Load();
}

1: n way_node , not node (/). , , node.

, 1 β†’ 300 , β†’ 2000 . , , 1 + 300 + 300 * 2000... , .

? , . ; , ?

+4
2

:

IQueryable<relation> query = context.relations;
query = query.Where( ... ); // filters down to exactly one
query = query.Include(r => r.relation_members
    .Select(rm => rm.way.way_nodes
        .Select(wn => wn.node)));
relation rel = query.SingleOrDefault();

, , Include ...Select(rm => rm.way) , , . ( , , - SQL , .)

, , - , .Include(r => r.relation_members.Select(rm => rm.way)) , . ?

:

foreach (relation_member rm in rel.relation_members) {
    context.Entry(rm).Reference(r => r.way).Query()
        .Include(w => w.way_nodes.Select(wn => wn.node))
        .Load();
}
+5

Include() - , //.

Include() Select() :

:

context.Invoices
  .Include(invoice => invoice .Positions)
  .ToList();

, :

context.Invoices
  .Select(invoice  => new {invoice, invoice.Positions})
  .AsEnumerable()
  .Select(x => x.invoice)
  .ToList();

, Include().

+1

All Articles