❔ 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
what does your addContact and updateContact look like?
not sure if it's the cause but you're definitely misusing the database
you can't assume that this will produce parallel lists
a contact should have a relationship to its phone number and be resolved through that
Introduction to relationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
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.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?
BlazeBin - ydossnfgpnce
A tool for sharing your source code with the world!
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, etcAh 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
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 entitygotcha, 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?
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
ah that makes sense
one example for this case would be configuring your model to automatically always include the phone numbers for each contact
Eager Loading of Related Data - EF Core
Eager loading of related data with Entity Framework Core
(you should only do this if you always need the phone numbers 100% of the time)
and this example is in regards to explicitly specifying relationships right?
yeah, it's related
since EF convention is not to include related entities by default you have to explicitly tell it you want that
got it
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.