How do I view the SQL generated by the Entity Framework?

后端 未结 22 2829
别那么骄傲
别那么骄傲 2020-11-21 05:35

How do I view the SQL generated by entity framework ?

(In my particular case I\'m using the mysql provider - if it matters)

22条回答
  •  迷失自我
    2020-11-21 06:07

    I am doing integration test, and needed this to debug the generated SQL statement in Entity Framework Core 2.1, so I use DebugLoggerProvider or ConsoleLoggerProvider like so:

    [Fact]
    public async Task MyAwesomeTest
        {
            //setup log to debug sql queries
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddProvider(new DebugLoggerProvider());
            loggerFactory.AddProvider(new ConsoleLoggerProvider(new ConsoleLoggerSettings()));
    
            var builder = new DbContextOptionsBuilder();
            builder
                .UseSqlServer("my connection string") //"Server=.;Initial Catalog=TestDb;Integrated Security=True"
                .UseLoggerFactory(loggerFactory);
    
            var dbContext = new DbContext(builder.Options);
    
            ........
    

    Here is a sample output from Visual Studio console:

提交回复
热议问题