❔ 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 (Erased)
This is how to models looks like
Aragubas (Erased)
When I try to load the data the Numbers field in contact is null, but the data exists in the database
Aragubas (Erased)
the error
Aragubas (Erased)
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 (Erased)
Ooo 👀 I will read it
Aragubas (Erased)
for some reason its searching for a table called PhoneNumbers but the correct name would be PhoneNumber (without the S)
Aragubas (Erased)
do I need to rename the model class?
SuperBrain
SuperBrain2y ago
You forgot to declare a DbSet for PhoneNumbers
Aragubas (Erased)
Oooo
SuperBrain
SuperBrain2y ago
It is a standard EF convention to pluralize table names while keeping your entity names in singular form.
Aragubas (Erased)
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
More Posts
❔ ❔ MVC - Chart - Model - NullReferenceExceptionI'm using google chart to display my data. The chart works by default, but when I change the foreach❔ Get id from query or DTOImagine I have the next two entities class Task{ public int Id {get;set;} public string Desc {get;s✅ Is it possible to check how long it would take for the battery to finish charging?Hi, I know i got a lot of information form my previous thread, but I would still like to access more❔ ✅ How do I import System.Windows.Devices.Power?I know I need to make a namespace, but how? It doesn't show up on the list and I just want to aggrav❔ Capture parameter value with Moq decorated with the 'in' keywordHello! I'm writing Unit Tests for a library with a lot of structs, which utilizes the `in` keyword t✅ Mention user in footer in Embed (.Net 6)Hi, I'm trying to mention the user to used the command in the footer but I can only make it write itMap complex object (dictionary) from appsettings.json to modelHi, i have a complex appsettings.json like this ``` "interceptor": { "interactionType": "redire❔ Passing a list of values to Stored Procedure in EF CoreHey devs, I would like to know whether it is possible to pass a list of values as a parameter to a ✅ Input into list (Console app)**Program** I want the user to keep creating accounts, and then put them inside at list **Example o❔ POST request in swagger shows entire schema and parts of it are not needed in request bodyI have a one to one relationship between Product and CartItem, in the POST request for Product the C