my .NET Core console app connects to an sqlite db

<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0-preview4.19176.6" />

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
   string connectionString = "Data Source=\\mydb.db;";

var q = dbc.mytable.Select(x => x);

How can I get the underlying SQL query used by q ?

If I try q.ToString() I get:


Any help ?

1 Answers

user10103607 On

Suppose your IQueryable instance is as per your example:

var q = dbc.mytable.Select(x => x);  // your sample IQueryable object

For EntityFramework versions prior to EF Core, this would have worked

var sqlQuery = ((System.Data.Objects.ObjectQuery)q).ToTraceString();

and for EF 6, this would have worked

var sqlQuery = ((System.Data.Entity.Core.Objects.ObjectQuery)q).ToTraceString();

Sorry that I didn't look at the tags of your question earlier which mentions EF core.

In that case I found this article which provides few options to achieve the objective if using EF Core.