C
C#17mo ago
GIGA BRAIN

❔ Program showing unwanted entries after multiple uses

Whenever I run my method that returns all data entries from my database, it shows me extra entries at the top, but they are obviously incorrect. I don't know what is causing this, I have console.clear() at the beginning of the method, so I'm thinking it could be something with my for loop? It seems right to me though, so I'm not sure. Any ideas? pastebin: https://paste.mod.gg/xvhmdmemrfvn/1 Reference in menu class; lines 34-38
BlazeBin - xvhmdmemrfvn
A tool for sharing your source code with the world!
23 Replies
friedice
friedice17mo ago
what does your addContact and updateContact look like?
Jimmacle
Jimmacle17mo ago
not sure if it's the cause but you're definitely misusing the database
Jimmacle
Jimmacle17mo ago
you can't assume that this will produce parallel lists
Jimmacle
Jimmacle17mo ago
a contact should have a relationship to its phone number and be resolved through that
Jimmacle
Jimmacle17mo ago
Introduction to relationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
Accord
Accord17mo ago
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. 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.
GIGA BRAIN
GIGA BRAINOP17mo ago
so i'm trying to fix this, but am not sure if my models are incorrect or if the code that gets information from the tables is wrong would you mind taking a look at it again?
GIGA BRAIN
GIGA BRAINOP17mo ago
BlazeBin - ydossnfgpnce
A tool for sharing your source code with the world!
GIGA BRAIN
GIGA BRAINOP17mo ago
GIGA BRAIN
GIGA BRAINOP17mo ago
Jimmacle
Jimmacle17mo ago
your models look right, it's just your code doesn't access them correctly if you want to get all your contacts and their phone numbers, you should do something like _ctx.Contacts.Include(x => x.Numbers).ToList() then access each contact's numbers through the contact.Numbers collection this will make sure that you access the numbers that actually belong to each contact, accessing each full list in parallel like your original code does will break as soon as you start adding/removing numbers or some contacts have 2, etc
GIGA BRAIN
GIGA BRAINOP17mo ago
Ah so thats what i did wrong i didn't know about the include part so whenever i want to access a sub-property, i have to include(x => x.PropertyHere)? and in this case, the x is a pointer to the Numbers property in my contact class from which I can then access the list in there
Jimmacle
Jimmacle17mo ago
only for navigation properties, since resolving related data changes the SQL it's not done by default and only if you don't explicitly access the data in a .Select or something the x is part of the expression that tells EF core what navigation property you want to include, in this case x refers to an instance of your Contact entity
GIGA BRAIN
GIGA BRAINOP17mo ago
gotcha, thanks for the help and explanations wait one more question, i read in the link you sent earlier that i can also specify the relationships explicitly if i override the OnModelCreating method in dbcontext would it be the same steps to access the data if i did the onmodelcreating part? i dont have one in this project, but is including that method a good practice?
Jimmacle
Jimmacle17mo ago
yes, if you did it explicitly you'd access the data the same way but right now EF can infer the correct configuration by convention generally you want to follow EF conventions and only explicitly configure things that it can't figure out or aren't default
GIGA BRAIN
GIGA BRAINOP17mo ago
ah that makes sense
Jimmacle
Jimmacle17mo ago
one example for this case would be configuring your model to automatically always include the phone numbers for each contact
Jimmacle
Jimmacle17mo ago
(you should only do this if you always need the phone numbers 100% of the time)
GIGA BRAIN
GIGA BRAINOP17mo ago
and this example is in regards to explicitly specifying relationships right?
Jimmacle
Jimmacle17mo ago
yeah, it's related since EF convention is not to include related entities by default you have to explicitly tell it you want that
GIGA BRAIN
GIGA BRAINOP17mo ago
got it
Accord
Accord17mo ago
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.
Want results from more Discord servers?
Add your server