Entity Framework 6 – How can I view the SQL that will be generated for an insert before calling SaveChanges

Another option (if I understand your question correctly), would be to use an IDbCommandInterceptor implementation, which seemingly allows you to inspect SQL commands before they are executed (I hedge my words as I have not used this myself).

Something like this:

public class CommandInterceptor : IDbCommandInterceptor
{
    public void NonQueryExecuting(
        DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        // do whatever with command.CommandText
    }
}

Register it using the DBInterception class available in EF in your context static constructor:

static StuffEntities()
{
    Database.SetInitializer<StuffEntities>(null); // or however you have it
    System.Data.Entity.Infrastructure.Interception.DbInterception.Add(new CommandInterceptor());
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)