EF sets a property out of nowhere [Solved] [Answered]
I have a model with this constructor. Basically, I get the amount of replies which are not deleted through Count. Total is 5 and 3 of them are not deleted.
Literally the same codes. But at the end, RepliesCount returns 5 and RepliesLength returns 3. RepliesLength can have any name. But when I use the name "RepliesCount", it fetches all the replies.
I used breakpoints to see where it comes from since I didn't reference to it anywhere. It starts filling properties, assigns 3 to both, as one would expect. But after filling others properties, for some reason it jumps to setter and assigns 5 to RepliesCount.
My theory; EF can fill the Replies if I want. Perhaps if I write the "Replies" then add "Count" at the end, it automatically runs the Count function and assigns to it? Because I didn't reference it anywhere.
24 Replies
EF will respect constructors if it can figure out which one to use (names etc match properties), but it can also just set the props directly
you might be running into a situation where it does both
generally, you either provide a ctor that has matching arguments for all the database-bound fields, or an empty ctor
well, i wanted to do some quick calculations inside the constructor of dto so I would have an easy way of using them later. Like in the api controller. I don't want to loop through the comments and give them the number of replies after all
i am not sure which one is faster but
let me try the same thing with some other list.
votes, come here
I already have VoteResult. Which is the small calculation of upvotes and downvotes. I will create a property called VoteCount
Nope. "voteCount": 0. It should give me 2
sorry it should be VotesCount
there is still a chance 😄
LMAO It worked
"votesCount": 2,
@Pobiega
dammit EF. How dare you make me a monkey for 5 hours
Do you keep these DTOs in your database?
at that point, they are not DTOs anymore - they are entities
Not database. Api
I am not sure if you meant api there
I meant database.
why would EF be invovled with your API at all?
EF is used to talk to the database only.
You seem like a knowledged person. So I ruled out most of my guesses. Did I understand correctly? Api speaks to database. And I am just calling the api to give me data.
I don't have EF in my app. It's for api
Mhm.. Perhaps its better if you just show the code where you use this?
I interpreted your original post as if you thought EF was calling your constructor by itself and/or assigning the props
I am approaching home. A couple of things to do, at leadt 30 minutes sadly
but thats obviously not the case, since this is a DTO
No no
Dtos are in the api. My api uses ef to speak to database
I just created the models I need in my app.
Sure. Well, get home, do whatever you need, then come back and post the code where you use this DTO
this.RepliesCount = ...
will call the setter, btw
just so you know
it obviously shouldn't be sticking a 5 in there, so we'll need to see the full code to figure out whyIt's an extra property. I fixed it already, sorry I didn't changed the title
I don't fetch the replies. I only need its count. Writing the List type property name and adding "Count" to it like "RepliesCount" brings the count of that list, overriding the previously assigned values. It took quite a while to learn that.
I simply changed the Count with Length.
That is not automagic.
Are you using some kind of mapping library?
Automapper, Mapster?
Yes. Obviously there is EF magic.
Autonapper
EF isnt involved, you said so yourself
I don't know. But EF is the one that checks the names and assigns stuff
If your controller/service/handler code is
then EF never touches your dto at all
You dont need to map the property explicitly, automapper interprets the property name and auto map Projects.Count() to ProjectsCountfound this in a SO comment, and its pretty much what I expected If your model has a
Replies
prop, and your DTO has a RepliesCount
prop, it will autofill
Automapper is sneaky, it has a lot of default behaviour that you need to considerSo it's automapper magic 😄
yup!
now i can sleep peacifully. Thanks, take care
I've kinda jumped ship from AutoMapper lately, because of things like this
✅ This post has been marked as answered!
(we have a bot command to mark questions as closed,
/close
)I see