❔ Adding database entries with EF Core
I'm completely new to EF Core. I have two tables, Contact and Number. The two tables are connected through FK ContactId in the Number table. I want to create an entry but not sure how I would be able to add information to both tables, here is the pastebin:
https://paste.mod.gg/cilvbumgkbvh/0
(Also, does anyone know how to make a field a foreign key after i've done the add-migration and update-database?)
BlazeBin - cilvbumgkbvh
A tool for sharing your source code with the world!
13 Replies
This works:
but i feel like there is a much better way to do it
There is,
.SaveChangesAsync()
instead of .SaveChanges()
But besides that, LGTM
Or, if you have nav properties set up correctly, you can do
hey as a side note, if you're really new to database and ef core, implement the repository and unit of work patterns. i wish i learned that instead of having to go back and learn it now
Fuck no
It adds unnecessary complexity
And is completely useless code
EF already is a repository and does implement UoW
Ok i agree with you one hundred percent but the job market asks for uow
if this program is not going to be a web app, would i be good just sticking with saveChanges()?
If you really want to have blocking code, then sure, go ahead and use it
by blocking code do you mean how the code runs sequentially
i thought you only used async for web apps or stuff that run things simultaneously
can console apps still use async stuff?
Of course
ok bet
will use async
sorry another question
i could achieve this if i just make an instance of numbers in the contact class right?
is that what you mean by nav properties?
Yes
That's how relationships between tables are created with EF
ah 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.