Is it possible to get query that entity framework generated for stored procedure before execution? E.g. calling sp
context.Test(1)
and get the string
exec [dbo].Test 1
but before execution
Is it possible to get query that entity framework generated for stored procedure before execution? E.g. calling sp
context.Test(1)
and get the string
exec [dbo].Test 1
but before execution
If you're using Entity Framework 6, then you can use Query Interceptors to inject code between SQL generation and SQL execution. It is done by implementing IDbInterceptor
. You can attach to the following "events":
namespace System.Data.Entity.Infrastructure.Interception
{
public interface IDbCommandInterceptor : IDbInterceptor
{
void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext);
void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext);
void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext);
void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext);
void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext);
void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext);
}
}
You can write custom interceptor implmenting the interface above and then add it into your EF by calling:
DbInterception.Add(new <your implementation>());
There are also other suggestions of viewing the SQL generated by Entity Framework here: How do I view the SQL generated by the Entity Framework?, but it depends whether you want to just view the SQL or to perform some actions before it is executed.