I have a User entity with a collection of Roles that I am trying to load.
I execute this statement...
_Context.Entry(user).Collection(x => x.Roles) .Load();
The User's Roles are loaded. I can trace the database and see that 11 rows are returned and 11 Roles are in the collection.
I am trying to load the Roles based on other criteria but in my debugging I have found that the following statement...
_Context.Entry(user).Collection(x => x.Roles) .Query().Load();
Will return the same exact results as in the previous example but the Roles collection doesn't have 11 items, it has 0.
Even if I do a super simple where clause such as...
_Context.Entry(user).Collection(x => x.Roles) .Query().Where(x => x.RoleId > 0).Load();
11 rows are returned from the database but zero items are added to the collection.
Comments: You nailed it! It is indeed a many to many relationship. I am trying to load only a subset of the roles for a user. I have a list of roles that I want to load for a user, not all of them. Something like this... ``` _Context.Entry(user).Collection(x => x.Roles) .Query() .Where(x => myCollection.Contains(x.RoleId)) .Load(); ``` The SQL generated is spot on but as you explained, the results are not loaded. Do you have any suggestions on how to load my Roles collection? Thanks
I execute this statement...
_Context.Entry(user).Collection(x => x.Roles) .Load();
The User's Roles are loaded. I can trace the database and see that 11 rows are returned and 11 Roles are in the collection.
I am trying to load the Roles based on other criteria but in my debugging I have found that the following statement...
_Context.Entry(user).Collection(x => x.Roles) .Query().Load();
Will return the same exact results as in the previous example but the Roles collection doesn't have 11 items, it has 0.
Even if I do a super simple where clause such as...
_Context.Entry(user).Collection(x => x.Roles) .Query().Where(x => x.RoleId > 0).Load();
11 rows are returned from the database but zero items are added to the collection.
Comments: You nailed it! It is indeed a many to many relationship. I am trying to load only a subset of the roles for a user. I have a list of roles that I want to load for a user, not all of them. Something like this... ``` _Context.Entry(user).Collection(x => x.Roles) .Query() .Where(x => myCollection.Contains(x.RoleId)) .Load(); ``` The SQL generated is spot on but as you explained, the results are not loaded. Do you have any suggestions on how to load my Roles collection? Thanks