Home

Entity Framework DbCommandInterceptor vs. SQL panel (LINQPad 5)

edited October 2019
If I use an Entity Framework DbContext connection in LINQPad 5, define and register an own DbCommandInterceptor, then the result of the command interceptor does not show up in LINQPad's SQL panel.

The actual interception does happen, and its result can be shown in LINQPad Results panel by hooking onto dbContext.Database.Log.

It seems that the SQL commands get to the SQL panel too soon, i.e. before the command interceptors could intercept them.

It would be great if the SQL commands got to the SQL panel after the command interceptors intercepted them.

Comments

  • edited October 2019
    I forgot to tell you that I experienced this behavior in LINQPad 5. I do not know what is the situation in LINQPad 6. I have edited my post so that it now reflects the fact that it is related to LINQPad 5.

    This is my code in LINQPad:

    void Main()
    {
    DbInterception.Add(new MyInterceptor());

    using (var dbContext = new MyDbContext())
    {
    dbContext.Database.Log += command => command.Dump();

    dbContext.People.Dump("list");
    }
    }

    public class MyInterceptor : DbCommandInterceptor
    {
    public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext)
    {
    command.CommandText = command.CommandText.Replace("SELECT", $"-- MyInterceptor{Environment.NewLine}SELECT");
    }
    }
    And this is my DbContext in a separate dll which is used through LINQPad's "Entity Framework DbContext" connection:

    public class MyDbContext : DbContext
    {
    public MyDbContext()
    : base("Data Source=sqlserver; Initial Catalog=UUU; Persist Security Info=True; User ID=myuser; Password=mypassword")
    {
    }

    public DbSet People { get; set; }
    }

    public class Person
    {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    }
    Inside the SQL panel I do not see the "-- MyInterceptor" text. Inside the Result window I do see it (thanks to "dbContext.Database.Log += command => command.Dump()").

    It does not matter whether MyInterceptor's implementation and registration are being done inside the code in LINQPad or in the separate dll (inside DbConfiguration); the behavior is the same in both cases.
  • @JoeAlbahari, please let me know if you need further information to reproduce the problem.
  • The behavior makes sense, because LINQPad installs its DbCommandInterceptor first.

    You can tell LINQPad not to write SQL translations by doing this in your setup code:

    ExecutionEngine.SqlTranslationsEnabled = false;

    Then in your ReaderExecuting method:

    Util.SqlOutputWriter.Write (command.CommandText);
Sign In or Register to comment.