You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm new to Entity Framework and something has been bothering me and I can't seem to solve it: whenever I request a table with a one to many or many to many relationship, the query requests the tables but also any table that are linked to it by a foreign key, and the tables that are linked to those tables, etc ...
For example: in my application I first added two classes with a many to many relationship : 'News' and 'Country'
public class News
{
public int? Id { get; set; }
[Column(TypeName = "nvarchar(255)")]
public string? Title { get; set; }
public DateTime? DatePublication { get; set; }
[Column(TypeName = "text")]
public string? Content { get; set; }
public bool Enabled { get; set; } = false;
[NotMapped]
public List<Country>? Countries { get; set; }
}
public class Country
{
[Key]
public int? Id { get; set; }
[Column(TypeName = "nvarchar(255)")]
public string? Title { get; set; }
[Column(TypeName = "nvarchar(10)")]
public string? ISOCode { get; set; }
[NotMapped]
public List<News>? News { get;set; }
}
This was already problematic as when I tried to query the news, it would include the country, but it would try to get the news related to this country, resulting in an infinite loop. I solved that problem by ignoring cycle in the json options.
But then when my application got more complicated, the query got out of hands. I added a continent classes with a one to many relationship with news and country
public class Continent
{
[Key]
public int? Id { get; set; }
[Column(TypeName = "nvarchar(255)")]
public string? Title { get; set; }
[NotMapped]
public List<Country>? Countries { get; set; }
[NotMapped]
public List<News>? News { get; set; }
}
Now when I request the news, it also requests its country but also every other news this country has as well as its continent. It also request the continent of the news but also every of its countries and all the news linked to those countries.
I thought it was due to lazy loading, so I added this line of code in the DBContext:
@HadrienAllemon EF should only be loading the entities you request. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
I'm new to Entity Framework and something has been bothering me and I can't seem to solve it: whenever I request a table with a one to many or many to many relationship, the query requests the tables but also any table that are linked to it by a foreign key, and the tables that are linked to those tables, etc ...
For example: in my application I first added two classes with a many to many relationship : 'News' and 'Country'
This was already problematic as when I tried to query the news, it would include the country, but it would try to get the news related to this country, resulting in an infinite loop. I solved that problem by ignoring cycle in the json options.
But then when my application got more complicated, the query got out of hands. I added a continent classes with a one to many relationship with news and country
Now when I request the news, it also requests its country but also every other news this country has as well as its continent. It also request the continent of the news but also every of its countries and all the news linked to those countries.
I thought it was due to lazy loading, so I added this line of code in the DBContext:
But it didn't change anything. Am I missing something?
Here's how I request the news :
The result I expected :
And the result I get :
Any help is greatly appreciated
Include provider and version information
EF Core version: 6.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Widnows Server
IDE: Visual Studio 2022
The text was updated successfully, but these errors were encountered: