C
C#12mo 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
friedice12mo ago
what does your addContact and updateContact look like?
Jimmacle
Jimmacle12mo ago
not sure if it's the cause but you're definitely misusing the database
Jimmacle
Jimmacle12mo ago
you can't assume that this will produce parallel lists
Jimmacle
Jimmacle12mo ago
a contact should have a relationship to its phone number and be resolved through that
Jimmacle
Jimmacle12mo ago
Introduction to relationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
Accord
Accord12mo 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 BRAIN12mo 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 BRAIN12mo ago
BlazeBin - ydossnfgpnce
A tool for sharing your source code with the world!
GIGA BRAIN
GIGA BRAIN12mo ago
GIGA BRAIN
GIGA BRAIN12mo ago
Jimmacle
Jimmacle12mo 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 BRAIN12mo 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
Jimmacle12mo 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 BRAIN12mo 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
Jimmacle12mo 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 BRAIN12mo ago
ah that makes sense
Jimmacle
Jimmacle12mo ago
one example for this case would be configuring your model to automatically always include the phone numbers for each contact
Jimmacle
Jimmacle12mo ago
(you should only do this if you always need the phone numbers 100% of the time)
GIGA BRAIN
GIGA BRAIN12mo ago
and this example is in regards to explicitly specifying relationships right?
Jimmacle
Jimmacle12mo 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 BRAIN12mo ago
got it
Accord
Accord12mo 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
More Posts
❔ App is sometimes slow after starting, but fast other times(This is a WPF app) I don't really know how else to describe it other than sometimes when I start th❔ getting all methods with an attribute in an optimized wayhey all, im trying to find a way to add all methods with an attribute to dictionary. i want to be ab❔ anyone interested in helping a starter?Im a starter looking to start learning c#. after spending days researching where to start im still k❔ How to mock HttpClient that is automatically applied in class without IHttpClientFactoryHello, I have a problem because I don't know how can I mock ``HttpClient`` in my tests for ``StripeC❔ Textmate themes for custom languages?So it's apparently possible to add support for custom syntax highlighting via textmate grammars acco❔ My Label Doesn't show up even though no errors.Hi, I am making like PC Stats app like HWInfo or something like that, but I am using Open Hardware M❔ NuGet broken, i have no ideaSo it has been ages since i coded in c#, turns out my NuGet is broken or something❔ C++ Program displaying same output regardless of input value.Attached is my code and the output I receive with inputs "5000", "20000", and "34567" The program is❔ Learning ArchitecturesHi! Can you recommend any books, articles, links to github that will help me better understand all k❔ (Due in about two days) Fish Project for C# UnityHiya! I'm new to C# and im currently in university for Game Development in two days I have this proj