Using EF Core (or any ORM for that matter) I want to keep track of the number of queries the ORM makes to the database during some operation in my software.
I\'ve us
You can use a bounded context. I used EF Coed first to create two different contexts
Customer bounded context will not log any queries
public class CustomerModelDataContext : DbContext
{
public DbSet Customers { get; set; }
public DbSet PostalCodes { get; set; }
public CustomerModelDataContext()
: base("ConnectionName")
{
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
Database.SetInitializer(new Initializer());
//Database.Log = message => DBLog.WriteLine(message);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
API bounded context will log the queries
public class ApiModelDataContext : DbContext
{
public DbSet ApiTokens { get; set; }
public DbSet ApiClients { get; set; }
public DbSet ApiApplications { get; set; }
public ApiModelDataContext()
: base("ConnectionName")
{
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
Database.SetInitializer(new Initializer());
Database.Log = message => DBLog.WriteLine(message);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
this will log the query to debug output window in VS
public static class DBLog
{
public static void WriteLine(string message)
{
Debug.WriteLine(message);
}
}