❔ A problem with database in asp dotnet
I have class
Market
that has Owner
as one object. With migration everything worked just fine. In database Market table has value OwnerId
and writing to that table from program works just fine, but when i try to get those values it shows me null
in place of Owner. What do I do?27 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
this is
Market
this is where I get them from database
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
you mean using? I have all the Entities namespace.
I have this line on top.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
db.Market.Include(x => x.Owner).FirstOrDefaultAsync(x => x.Id == id);
Or use lazy loaded relationsthx a lot it worked
yeah did that. thanks to you too
will it still work when i have 1 to many relation using
List
?Yes
It just includes whatever relation you have
Usually your List would be an ICollection in the model definition
thanks a lot
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Lol
The dude's gonna read your link, don't worry. No problem with giving the actual reason
No idea what's so wrong with that
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I would agree if it's a big problem. This is just a small mistake he made
A bit shitty to provide all the documentation when it's such a small thing
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
So the next time you see something you don't agree with, you explain why you don't agree with it instead of responding with "don't do this".
I simply gave an alternative without providing context
Lazy loading, as the link explains, can indeed make or break your application
But considering he would have to look how to do it, I can imagine there being a mention of that wherever he looks for it
So...
1. Don't ever use lazy-loading. It'll query your database each time for each related property. And why query the database 273 times if you can query it once?
2. Don't use
.Include()
if you're fetching data for display only. Use .Select()
and select the data into a separate DTO/ViewModel class. That way, you only get the data you needUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
You're kind of required to use Include. I would advice on not using it altogether, and getting related data seperately. This is more scalable instead of getting everything and everything related at once, since you probably wont use half of it in most endpoints
Automapper has a ProjectTo<> extension that is pretty useful and saves the hassle of creating the Select statements yourself
No, you need to call Include, and then Select
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Automapper can't even load related data. I believe it's a bug
No you don't
There's zero need for includes with a select
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Guess it makes sense
Don't think I ever read that it was possible
This works perfectly fine
For a
Person
like
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Fetches all the data you need, and only the data you need, and does it in a single query
Lazy loading and
.Include()
eat your heart outWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.