C
C#2y ago
Aragubas

❔ EntityFramework Relations doesn't work

Hewoo, I'm new to EntityFramework and I'm trying to make a simple One to Many relation but it doesn't work
13 Replies
Aragubas
AragubasOP2y ago
This is how to models looks like
Aragubas
AragubasOP2y ago
When I try to load the data the Numbers field in contact is null, but the data exists in the database
Aragubas
AragubasOP2y ago
the error
Aragubas
AragubasOP2y ago
public class ContactContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=./database.sqlite");
}
}
public class ContactContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=./database.sqlite");
}
}
that's the ContactContext class what am I missing here to make the relation work?
Saber
Saber2y ago
when querying for an entity, the relationships are not loaded unless you include them. https://learn.microsoft.com/en-us/ef/core/querying/related-data/eager
Eager Loading of Related Data - EF Core
Eager loading of related data with Entity Framework Core
Aragubas
AragubasOP2y ago
Ooo 👀 I will read it
Aragubas
AragubasOP2y ago
for some reason its searching for a table called PhoneNumbers but the correct name would be PhoneNumber (without the S)
Aragubas
AragubasOP2y ago
do I need to rename the model class?
SuperBrain
SuperBrain2y ago
You forgot to declare a DbSet for PhoneNumbers
Aragubas
AragubasOP2y ago
Oooo
SuperBrain
SuperBrain2y ago
It is a standard EF convention to pluralize table names while keeping your entity names in singular form.
Aragubas
AragubasOP2y ago
public DbSet<PhoneNumber> PhoneNumber { get; set; }
public DbSet<PhoneNumber> PhoneNumber { get; set; }
after adding this line to the Context everything is working as intended! thanks Ooo 👀
Accord
Accord2y 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