Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stop Entity Framework from including related data automatically #27906

Closed
HadrienAllemon opened this issue Apr 28, 2022 · 3 comments
Closed

Comments

@HadrienAllemon
Copy link

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:

public DataBaseContext(IConfiguration configuration)
{
    Configuration = configuration;
    this.ChangeTracker.LazyLoadingEnabled = false;
}

But it didn't change anything. Am I missing something?

Here's how I request the news :

return (from news in _context.News 
        select news).Include(n => n.Countries)
                    .Include(n => n.Continents)
                    .ToList();

The result I expected :

[
  {
    "id": 1,
    "title": "test1",
    "content": "helo",
    "enabled": true,
    "countries": [
      {
        "id": 1,
        "title": "Belgium",
        "continentId": 1
      }
    ],
    "continents": [
      {
        "id": 1,
        "title": "Europe"
      }
    ]
  },
  {
    "id": 2,
    "title": "test2",
    "datePublication": "1994-11-15T00:00:00",
    "content": "helo",
    "enabled": true,
    "countries": [
      {
        "id": 2,
        "title": "Luxemburg",
        "isoCode": "lu",
        "continentId": 1
      }
    ],
    "continents": [
      {
        "id": 2,
        "title": "Asia",    
      }
    ]
  }
]

And the result I get :

[
{
    "id": 1,
    "title": "test1",
    "datePublication": "1994-11-15T00:00:00",
    "content": "helo",
    "enabled": true,
    "countries": [
      {
        "id": 1,
        "title": "Belgium",
        "continentId": 1,
        "continent": {
          "id": 1,
          "title": "Europe",
          "countries": [
            null,
            {
              "id": 2,
              "title": "Luxemburg",
              "continentId": 1,
              "continent": null,
              "news": [
                {
                  "id": 2,
                  "title": "test2",
                  "content": "helo",
                  "enabled": true,
                  "creator": "Hadrien",
                  "countries": [
                    null
                  ],
                  "continents": [
                    {
                      "id": 2,
                      "title": "Asia",
                      "countries": null,
                      "news": [
                        null
                      ]
                    }
                  ]
                }
              ]
            }
          ],
          "news": [
            null
          ]
        },
        "knowledge": null,
        "news": [
          null
        ]
      }
    ],
    "continents": [
      {
        "id": 1,
        "title": "Europe",
        "countries": [
          {
            "id": 1,
            "title": "Belgium",
            "continentId": 1,
            "continent": null,
            "news": [
              null
            ]
          },
          {
            "id": 2,
            "title": "Luxemburg",
            "continentId": 1,
            "continent": null,
            "news": [
              {
                "id": 2,
                "title": "test2",
                "content": "helo",
                "enabled": true,
                "countries": [
                  null
                ],
                "continents": [
                  {
                    "id": 2,
                    "title": "Asia",
                    "countries": null,
                    "news": [
                      null
                    ]
                  }
                ]
              }
            ]
          }
        ],
        "news": [
          null
        ]
      }
    ]
  },
  {
    "id": 2,
    "title": "test2",
    "content": "helo",
    "enabled": true,
    "countries": [
      {
        "id": 2,
        "title": "Luxemburg",
        "continentId": 1,
        "continent": {
          "id": 1,
          "title": "Europe",
          "countries": [
            {
              "id": 1,
              "title": "Belgium",
              "continentId": 1,
              "continent": null,
              "knowledge": null,
              "news": [
                {
                  "id": 1,
                  "title": "test1",
                  "content": "helo",
                  "enabled": true,
                  "countries": [
                    null
                  ],
                  "continents": [
                    null
                  ]
                }
              ]
            },
            null
          ],
          "news": [
            {
              "id": 1,
              "title": "test1",
              "content": "helo",
              "enabled": true,
              "creator": "Hadrien",
              "countries": [
                {
                  "id": 1,
                  "title": "Belgium",
                  "continentId": 1,
                  "continent": null,
                  "news": [
                    null
                  ]
                }
              ],
              "continents": [
                null
              ]
            }
          ]
        },
        "knowledge": null,
        "news": [
          null
        ]
      }
    ],
    "continents": [
      {
        "id": 2,
        "title": "Asia",
        "countries": null,
        "news": [
          null
        ]
      }
    ]
  }
]

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

@ajcvickers
Copy link
Contributor

@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.

@stevendarby
Copy link
Contributor

This looks related to fix up, this issue might be relevant: #11564

@ajcvickers
Copy link
Contributor

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.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants