❔ efcore querying
hello there iam right now digging a bit deeper into ef core and how to query data correctly but i dont get my head around something maybe someone can help
i have following data classes:
i have a table for both of these classes now my question is how do i handle following behaviour:
GetAllAsync(Guid? userId)
GetByIdAsync(Guid productId, Guid? userId)
goal is to include the rating the user gove if the userid isnt null if it is then return just the product model but i dont get around how to do it
this is what i tried
29 Replies
Do you want two separate queries,
GetAllAsync
as well as GetByIdAsync
? Or do you want to try and combine them into one query with a nullable userId
(like your GetByIdAsync
shows)?those are 2 seperate querys
GetAllAsync is a query which has a nullable argument userId if it is null then just return all products if userId is not null then include the rating from the user from the ProductRatings table
It doesn't look like the
Product
table has a defined relation to the ProductRating
table, so I think you might need to use a .Join
as part of your query to include ProductRating
, or add a navigational properties to your tables.i dont think so because i defined the relation in the model builder
Doesn't look like you have any relations defined. I'm assuming that a
Product
can have multiple ProductRating
?yes
more than one user can rate a product
So you're going to want to add some navigation properties and update the configuration to inform about the relationship:
Then you can include these in your query:
the userId i only get from the jwt token by my identity solution i dont save userdata myself
and you are right i forgot the forgein keys
Yeah, the User was only if you wanted to navigate to your user from ProductRating otherwise you can exclude it. Then you can query something like:
(code might need some cleanup as I just typed it in discord and not IDE)
hmm but how do i handle the extra property on the model classes now?
should i split them into database models and business layer models?
The extra property? You mean the
ProductRatings
on the Product
class and Product
on the ProductRating
table?yes
(iam migrating from dapper to ef core)
They're called Navigation properties, they don't actually create a column in your database as long as you've defined them as part of the relation.
Relationship navigations - EF Core
Reference and collection navigations in Entity Framework Core
thats not what i meant
i have a converter class to get from request to model and from model to response
Oh, like DTO to domain?
You can use projection in the query to map a domain model to another type. It looks something like:
no thats not related to the query part
the response and request only gets converted when i go outside from the business layer
i.e. at entrace to the api and at exit to the api
Does your
ProductResponse
have a property for the user rating, either an int?
or a ProductRatingResponse
object?
In ToContract
you would need to check for a null ProductRating
as part of the product
. If you have a int?
called UserRating
you would add if (product.ProductRating != null) UserRating = product.ProductRating.Rating;
to your ToContract
let me show
on api side things like userid and product id are also retrived via httpcontext and route parameters
Your
ProductResponse
has no idea about ratings, though.yea i still need to update that
the product response gives a nullable rating and userrating
So
ProductResponse
will have public int? UserRating { get; set; }
which is where you want to have the ProductRating.Rating
value stored if the user has rated the product, yes?yes
and the float? Rating will be the average of all ratings that there are
(for that product id)
So you're still going to want to have the navigation property on
Product
to link it to all of the ProductRating
. Then something like:
I think. Might need to tweak the Select stuff, though as I may have typos.hmm i think i gonna continue this tomorrow my brain stops working
thanks a lot tho
👍
Was 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.