djmurp
djmurp
CC#
Created by Ab on 2/15/2023 in #help
❔ Design Question
I don't think that would work because of circular references (Application references library, library references application), but you're right to be iffy about that anyway. It would be better design to have the library offer the state via a method, and then the project which contains the Application class (or even the Application class itself) can decide its own fate by calling the method which returns the state and setting it itself. This way the library isn't tied to Application and is now reusable in other places, and also you don't have a library changing state without you telling it when to do it
4 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
or follow the suggestions VS givees
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
you need to change your controller method (FavoritePerson) to return Task<IActionResult> and add the async modifier
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
so basically
var user = await _userManager.GetUserAsync(User);
_context.Entry(user) // lowercase user, NOT "User"!
.Collection(b => b.People)
.Load();
var user = await _userManager.GetUserAsync(User);
_context.Entry(user) // lowercase user, NOT "User"!
.Collection(b => b.People)
.Load();
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
yeah ignore me. I am confusing @jcotton42 s responses by confusing eager loading with explicit loading, think ill go to sleep now.
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
hmm
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
you cannot explicitly load a navigation property via the user manager GetUserAsync(User) method, so you have to do it alone via the _context
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
see above where I recommended the explicit loading
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
so the line below that line crashes?
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
it's empty because you are going via userManager, something provided to you via Identity. It doesn't automatically know that you want all related data so it gives you the basics (column values). Anything more would result in a lot of SQL JOINs
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
you can investigate it to see if it suits your use case, but feel free to persist and post an update instead
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
another approach would be entity framework lazy loading (in that case accessing .People will trigger additional SQL) but i'd avoid if possible - it's convenient but much less efficient!
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
check this https://stackoverflow.com/questions/39837355/eager-loading-using-usermanager-with-ef-core, the answer for you actually in the question itself
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
I have another code recommendation but we can solve this first
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
yes that should work in a readable way
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
then you will have to query
var userWithPeople = _context.ApplicationUsers.Include(x => x.People).FirstOrDefault(x => x.Id == user.Id);
var userWithPeople = _context.ApplicationUsers.Include(x => x.People).FirstOrDefault(x => x.Id == user.Id);
or similar after the userManager.GetUserAsync(User) call
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
yeh, or the equivalent property name in your context
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
do you know what table your users are in?
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
yep you've done the same thing, it's just that unfortunately you can't .Include that property when calling the built in Identity method - I thought you owned that code but you didn't
85 replies
CC#
Created by KooriByte on 2/5/2023 in #help
✅ Collection appears to be null, even though it's not null on DB
some properties are fine because they are columns in the same user table, but it won't go and get related entities from a different table (like .People)
85 replies