Duplicate objects in a DB Query
I'm querying entries from a table in a MySQL database with EFC.
When I look at the table in Rider (or via cli for that matter) the table looks like the first image. However, if I start it in debugger mode and look at what the database query returns in my code, I get the first entry in multiple positions
How is that even possible?


20 Replies
I've already dropped the table and created it so I really don't know whats happening here, the issue is not present on other tables which leads me to believe it's an issue related to the table
are you creating the table with efcore or manually with create table?
manually
I'm not yet that deep in this and basically just put efc on an existing mysql db
It seemed to work for some time but today it just suddendly broke
show the create table and how you mapped it in code
You have no primary key
i think you want a composite key there, and tell efcore about it. otherwise efcore will think the key is achievementId from the entity name
ok let me try
I also wonder how the database is structured
achievements
here seems to me like it would be a join table between User
and Achievement
But it's notSo we have a User table but the achievement id there is from a json file because they are managed by someone who isn't very familiar with dbs. So basically if a user triggers an event that would unlock an achievement, it will check the database if for that user, there already is an entry in this achievements table (which is also used to display a list of all achievements the user has unlocked)
I hope that isn't too confusing
Can an achievement be owned by only a single user?
Or do you have a table with actual achievements, and what you've shown is just a join table?
Ah
Wait
Yeah, you do
no anybody can unlock the achievement (I just realized something I missed to tell you: This table isn't a good representation of how it would normally look since in a normal test we'd only have one entry for each achievement for the user)
Yeah, you probably need a composite key to make sure that a given pair of
(UserId, AchievementId)
cannot be duplicatedok that makes sense
I'd still recommend you go code-first though
Having to handle the database separately from the code is a pita
can you explain what you mean with that?
Code-first: you declare your classes in code, create a migration, and that generates the database
oh I see
when I first heard of that it sounded way too scary so I just decided to not bother -- in hindsight not the smartest way of thinking
For example,
will create a table
Skungas
with primary key Id
of type INT
, column Name
of type TEXT
, and foreign key to Bungas
tablethank you so much for the help