简体   繁体   中英

Selecting multiple relationships in Entity Framework

I have table BaseTable which has the list Categories ( Category table). Categories have a list of Definitions ( Definition table), Definitions have a list of Values ( Value table) and Definitions table also has a list of Commands ( Commands table).

Below is the table structure:

BaseTable: Categories -> Definitions -> Values -> Comments
BaseTable: Categories -> Definitions -> Commands

So I want to retrieve a row from BaseTable along with related tables.

This is the code snippet I used to retrieved the data from the tables:

BaseTable base = base.dbcontext.BaseTables
    .Include(b1 => b1.Categories.Select(c => c.Definitions
                                        .Select(v => v.Values
                                        .Select(com => com.Comments))))
    .Include(b2=>b2.Categories.Select(k=>k.Definitions
                                       .Select(f=>f.Commands))) 
    .Where(b3 => b3.Id == id).SingleOrDefault();

Using above query I was able to retrieve the all the related tables for BaseTable . But in my database I have lot of entries in Values and Commands tables, so this query is taking lot of time to retrieve all the data.

Can anyone please help me in writing the correct query to retrieve the above scenario?

You can try lazy loading by adding the 'virtual' keyword to the properties in your model. In many cases it will improve the performance of your application.

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