ā Retrieving GUID from db with EF core returning 0s
Hi, I'm new to EF core and I don't understand why this issue occurs. So the data in db is fine. But when calling this:
replacing all T with OrderReadDto which is this:
The returned data is this:
Any idea? Thanks in advance.
27 Replies
If it's a DTO, why does it have attributes specific for database entities...?
Also, how do you get that data?
The code, specifically
oops yeah it wasn't supposed to be there
Ah, oof, repository pattern
Well, I don't see the DTO being used anywhere
Ah, nevermind
You're doing the mapping... client-side
With... automapper?
I'd say it's something wrong with the automapper, then
Perhaps there's no mapping between
TRead
and TReadDto
yeah with automapper... I will recheck the mapper but I do have Order -> OrderReadDto. However in OrderReadDto I only wanted the UserID from User object instead of the whole user details. I think I have done it wrongly but I'm not sure how to do it correctly
Does
BaseEntityWithId
have a UserId
property of Guid
type?
Or do you map it through User
, with User.Id
?So what I did was, I retrieved the user id from JWT via HTTP context and then GUID parse'd it. then added it manually to OrderCreateDto I wrote below.
So now OrderProduct doesn't actually have an user object. And Order has User in it for the id but I suppose it actually contains the whole User instead of just the id...
BaseEntityWithId:
and User:
Order (not dto):
DTO for creating order:
if my assumption is correct is it possible somehow to get only the user id in the dto somehow....
If the mapping in the automapper is correct, yes
Or you can just use a
.Select()
and not be at automappers black box of a mercyYea I do have this in my mapper profile
So i thought maybe this would work in the conversion
I have to google how to use Select... I just learned this whole web development in c# thing 2 weeks ago
It should work, yeah
But you're using...
TReadDto
?
Whatever that is?
And the mapping is to OrderReadDto
TReadDto is just a placeholder for the base service/etc so that it knows it will accept/return type of OrderReadDto/UserReadDto etc etc
it's magic to me
A generic repository...?
yes
I mean, yeah, it's way overcomplicated
Generic repository is just reimplementing what EF already gives you
This is a generic repository
like this is what is in my baserepo for example
bro to be honest i wish i can just go back to writing typescript for webdev but I committed to this
Yeah, you're just reimplementing stuff that already exists
Literally taking a spoon and making a spoon out of it
unfortunately i am required to do things this way š«
Ah, oof, my condolences
i'm so lost at this point i kinda wish that restrating my pc would somehow solve this problem
In that case, not sure how much of a help I'll be. I was never a fan of complexity for the sake of complexity, never used generic repositories
no worries, you tried and I'm thankful for that
Ah, wait, I have an idea
Two, rather
One: add a
UserId
property to your Order
, of type Guid
This will be the foreign key, and automapper should pick it up
Two: Since the mapping is done on the client instead of on the database, you might have to .Include(order => order.User)
to actually load the data that can later be mappedOk I will try that
š šØ
also i have just realised that
User id in the actual user db is stored as id and not userid
might it be that ef core somehow got confused
Yeah, but automapper will pick it up
User.Name
will be put in UserName
, User.Role.Color
will go into UserRoleColor
and so on
So that one should not be an issueSo
i just added step one
did new migration and db update and it works
but why?
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.