EF Core - Producing unfiltered queries on all members of dbcontext

Viewed 214

I'm writing this here, in the hope that someone has experienced this before and has a thorough answer to this problem.

We have a C# Web API that handles CRUD operations where we are using EF Core as our ORM to manipulate data.

From inspecting the SQL logs we can see that there are many select statements that are unfiltered (no where clause). That don't seem to be coming from the code base.

What is strange about this is some of the tables that are being called have no other references in the code other than being on the DBContext itself.

For example.

public class MyContext : DbContext{
    public DbSet<MyDbSet> MyDBSet { get; set; }
}

This seems to periodically output a select statement on the table even if its not used in the code.

SELECT [m].[Id], [m].[Column1], [m].[Column2]
FROM [MyDBSet] AS [m]

The problem with this is, that some of these tables have large amounts of data in which I would like to avoid an unfiltered query.

From further inspection it seems that the order in which these unfiltered queries occur is the same order they are found on the DBContext.

Example 2

public class MyContext : DbContext{
    public DbSet<MyDbSet> MyDBSet { get; set; }
    public DbSet<MyDbSet2> MyDBSet2 { get; set; }
    public DbSet<MyDbSet3> MyDBSet3 { get; set; }
}
SELECT [m].[Id], [m].[Column1], [m].[Column2]
FROM [MyDBSet] AS [m]

SELECT [m].[Id], [m].[Column1], [m].[Column2]
FROM [MyDBSet2] AS [m]

SELECT [m].[Id], [m].[Column1], [m].[Column2]
FROM [MyDBSet3] AS [m]

This happens even when the table is not read or referenced in the codebase, and there are no references to these tables that don't have a where clause.

My first assumption is that EF core is taking a snapshot of the table? Perhaps query caching or something similar?

I guess my question is, does anyone know what could be causing such odd behaviour and or is there something in EF core that is responsible.

NOTE - We are not using Lazy Loading Proxies package and are using eager loading throughout the application

1 Answers

After scrutinising a significant memory dump of the system - The package that was causing the reported unfiltered queries was Serilog.

https://github.com/RehanSaeed/Serilog.Exceptions

From here you will find the following details around versions of Serilog exceptions and it appears that being on below version 8 in conjunction with EF core, caused Serilog to try and destructure values that implemented IQuerable - in newer versions this does not happen.

WARNING: In versions of Serilog.Exceptions older than 8.0.0, if you are using EntityFrameworkCore with Serilog.Exceptions you must add this, otherwise in certain cases your entire database will be logged! This is because the exceptions in Entity Framework Core have properties that link to the entire database schema in them (See #100, aspnet/EntityFrameworkCore#15214). Newer versions of Serilog.Exceptions avoids this issue by preventing the destructure of properties that implement IQueryable preventing their execution.

Related