简体   繁体   中英

Loading three tables using Entity Framework include and lazy loading

I am using Entity Framework code-first, I have 3 tables, TableA is the master table, TableB is master table of TableC . I have reference key of TableA in TableC , it means to load TableB , I have to go through TableC .

TableA has columns PKA, Col1A, Col2A TableB has Columns PKB, Col1B, Col2B TableC has Columns PKC, Col1C, Col2C

Can someone please let me know how I can incorporate in my Linq query in Entity Framework and load all those three tables using include statement and write a Linq query, any help please - thanks in advance.

As i take it, TableC has dependency to both A and B. and you want to query TableC for its values along with respective values from A,B. If my assumption is right then you are looking for:

var list = context.TableC
    .Include(t => t.TableACollection)
    .Include(t => t.TableBCollection);

for selecting only certain fields from those tables:

var list = context.TableC
    .Include(t => t.TableACollection.Select(c => c.Col1A))
    .Include(t => t.TableBCollection.Select(c => c.Col1B));

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM