How to register SQL using framework 4.3 entity (code first) and Azure SQL Database

I understand that this similar question was asked several times, and I unsuccessfully tried recommendations in these questions.

I use the entity framework (4.3) and work against SQL Azure (in the federated database). I would like to be able to register the SQL that is generated by the entity framework.

I used the Entity Profiler Framework , and although this is useful during development, I am not sure if this would be useful during production.

I cannot use SQL Profiler as it is an Azure SQL Database.

I tried using EFTracingProvider and followed the steps here . Unfortunately, when I try to execute my first command (which uses the appropriate federation), I get an exception indicating that "The specified method is not supported."

The code that generates the error is as follows:

public MyContext(int tenantId) : base(CreateTracingConnection("TheDb"), true) { const string FederationCmdText = "USE FEDERATION TenantFederation(CustomerId = {0}) WITH RESET, FILTERING=ON"; ((IObjectContextAdapter)this).ObjectContext.EnableTracing(); ((IObjectContextAdapter)this).ObjectContext.Connection.Open(); // This is the line which throws the exception this.Database.ExecuteSqlCommand(string.Format(FederationCmdText, tenantId)); } 

Here's the exception:

 Specified method is not supported. at EFProviderWrapperToolkit.DbConnectionWrapper.CreateDbCommand() at System.Data.Common.DbConnection.CreateCommand() ... 

So here are my questions:

  • Is EFTracingProvider preferable for logging SQL queries (globally)?
  • If yes, any ideas why I get the exception above?
  • If not, is there another mechanism that will allow me to register all the SQL generated by the Entity Framework?

Thanks for your help, Eric.

+4
source share
1 answer

The problem I ran into was that, in my opinion, EFTracingProvider does not support running SQL directly. To solve this problem, one of the solutions is the following (what I found in Q & A from here )

Create the following class:

 public class DbTracingConnection : EFTracingConnection { protected override DbCommand CreateDbCommand() { return this.WrappedConnection.CreateCommand(); } } 

When creating a connection, use the above class instead of EFTracingConnection.

+5
source

Source: https://habr.com/ru/post/1396528/


All Articles