Home > Blockchain >  How to load data of a relation ship (many to many) ASP.Net Core 6 MVC
How to load data of a relation ship (many to many) ASP.Net Core 6 MVC

Time:08-22

i have a model called User and another with name SecurityGroup, these models have a relationship many to many in the database and exists a table for that relationship with name UsersSecurity...Ok, when i Scaffolded the model the model users autogenerate this attribute public virtual ICollection<UsersSecurity> UsersSecurities { get; set; }. I think this attribute can store the records with relation but when i use the debug view in a breakpoint i realizes that the attribute it's empty
This is the view in the database of user with UserID = 2 enter image description here
As you can see, there is two records with UserID = 2
But in the debug view in Visual Studio i notice this: Debub breakpoint
The attribute UsersSecurities have nothing inside, in the upper part of the photo you can see i'm searching the user with Id = 2, Line: var sc = _context.Users.Find(2);, ¿How can i bring the SecurityGroups associated to it in that variable? (I think there's information missing, just ask me for the information neccesary, please i reallly need solve this)

CodePudding user response:

If you are using lazy-loading, you must use Include for fetch related entites.

_context.Users.Include(u => u.UsersSecurities)....
  • Related