C
C#17mo ago
GIGA BRAIN

❔ 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
GIGA BRAIN
GIGA BRAINOP17mo ago
This works:
Contact JohnDoe = new Contact()
{
// ContactId will be 1
FirstName = "John",
LastName = "Doe"
};
context.Contacts.Add(JohnDoe);

Number JDoe = new()
{
ContactId = 1,
PhoneNumber = "313-3942-3013",
Description = "Co-worker"
};
context.Numbers.Add(JDoe);

context.SaveChanges();
Contact JohnDoe = new Contact()
{
// ContactId will be 1
FirstName = "John",
LastName = "Doe"
};
context.Contacts.Add(JohnDoe);

Number JDoe = new()
{
ContactId = 1,
PhoneNumber = "313-3942-3013",
Description = "Co-worker"
};
context.Numbers.Add(JDoe);

context.SaveChanges();
but i feel like there is a much better way to do it
Angius
Angius17mo ago
There is, .SaveChangesAsync() instead of .SaveChanges() But besides that, LGTM Or, if you have nav properties set up correctly, you can do
var contact = new Contact {
FirstName = "John",
LastName = "Doe",
Numbers = new List<Number>() {
new() {
PhoneNumber = "313-3942-3013",
Description = "Coworker"
}
}
};
_context.Contacts.Add(contact);
await _context.SaveChangesAsync();
var contact = new Contact {
FirstName = "John",
LastName = "Doe",
Numbers = new List<Number>() {
new() {
PhoneNumber = "313-3942-3013",
Description = "Coworker"
}
}
};
_context.Contacts.Add(contact);
await _context.SaveChangesAsync();
Candyroll93
Candyroll9317mo ago
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
Angius
Angius17mo ago
Fuck no It adds unnecessary complexity And is completely useless code EF already is a repository and does implement UoW
Candyroll93
Candyroll9317mo ago
Ok i agree with you one hundred percent but the job market asks for uow
GIGA BRAIN
GIGA BRAINOP17mo ago
if this program is not going to be a web app, would i be good just sticking with saveChanges()?
Angius
Angius17mo ago
If you really want to have blocking code, then sure, go ahead and use it
GIGA BRAIN
GIGA BRAINOP17mo ago
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?
Angius
Angius17mo ago
Of course
GIGA BRAIN
GIGA BRAINOP17mo ago
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?
Angius
Angius17mo ago
Yes That's how relationships between tables are created with EF
GIGA BRAIN
GIGA BRAINOP17mo ago
ah 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