question

Benjoe-2576 avatar image
0 Votes"
Benjoe-2576 asked Benjoe-2576 answered

Entity FrameWork6 Load takes forever

I have a query which calls Load() after and it takes forever. Is there anyway I can optimize it. Below is my query
var EmpUsers = data.DeptRoles
.Include(x => x.ROLE)
.Include(x => x.USER)
.Where(x => x.Id == deptId && x.USER.Inactive == false);

   EmpUsers.Load(); -----> this takes forever
dotnet-aspnet-mvcdotnet-entity-framework
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Benjoe-2576, Welcome to Microsoft Q&A, I am not full understand your meaning. what is your type of EmpUsers. is it a collection? Where is the Load method?

0 Votes 0 ·

The Linq query is included in the question.

var EmpUsers = data.DeptRoles
.Include(x => x.ROLE)
.Include(x => x.USER)
.Where(x => x.Id == deptId && x.USER.Inactive == false);

I am calling the Load method on the query and the query is a collection

EmpUsers.Load();

0 Votes 0 ·

I have figured out the reason. The USER table is huge so I am going to load the query in pieces instead of one query.

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

How long is "takes forever"? How many records are returned? Can you provide the table schema and/or entity design? Maybe you need to add an index on the DeptRoles table?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Benjoe-2576 avatar image
0 Votes"
Benjoe-2576 answered

I have fixed the issue myself.

var EmpUsers = data.DeptRoles;
var roles = EmpUsers .Include(x => x.ROLE);
var newUsers = roles.Include(x => x.USER)
.Where(x => x.Id == deptId && x.USER.Inactive == false);
newUsers.Load();

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.