Models&sql
Hello, i created three new models: Tag UserTags PostTags
the usertags and posttags were meant to hold a many to many relation between post/users and tags
however idk why after applying migration only one table was created
and it includes UserId and PostId even tho the PostId is not in the Tag model
The userId is just to keep track of who added the tag
21 Replies
the jobId is the postid
could it be because i added
to the Job model?
If you want many to many between tag and user both the tag and the user need to have a collection of the other
So tag needs a collection of users, and user a collection of tags
The way you have it it's a one to many
(also, remove the
virtual
. It implies lazy loading and lazy loading bad)but i need some lazy loading
for?
afaik the main issue is lazy loading can't be asynchronous and it's not obvious that accessing a lazy loaded property could take a (relatively) long time
Ok so how can i achieve it manually?
like if i needed to
in some services
Why?
Load data eagerly
Project unto DTOs
Get all the data you need, and only the data you need, in a single db round trip
im confused, isnt that what lazy loading does
No
Lazy loading will load the user first, in one query
Then, when you try to display blogpost by this user, it will query the db again
Then, for each post, it will query again for its tags
Instead of eagerly querying for the user, their posts, and those posts' tags, in a single database query
And if you use a select, or some other projection method, you can even prevent it from querying for user password and email when they're not needed
are you writing sql manually
no, you can do this in EF with normal linq
for example
so you just add select method
where are you achieving lazy loading?
ah
i see it
can you use ,include instead?
i'm not achieving lazy loading, that's the point
lazy loading is bad
i'm eagerly loading exactly what i need in a single query, and yes you can .Include as well if you want the whole related entity without explicitly selecting it
Okay so i have this
in my user, and 2 days ago i was having difficulty lazy loading the role name inside the Role entity
it didn't load until i used .include in my query
so what am i doing wrong if it should lazy load on its own??
you aren't using the right terminology
do not use lazy loading at all
what we're suggesting is not lazy loading
yeaa i know
but i just was wondering what i am doing wrong
because it should lazy load right?
no idea, i don't use lazy loading because it's bad
also i like the select query, it kindof also remove the need for mapping
resources
either .Include in your query or explicitly reference it in a .Select
or if you always want it 100% of the time, you can configure it to be included automatically
ah i just realized why it didnt lazy load
i think i didnt include it in the resources
damn
lol
i'm really confused what you're referring to as lazy loading
nevermind, but i got your point and ill remove the virtual
and start using .select
thank you