In Entity Framework Core, is it possible to see the SQL that will be applied when the SaveChanges()
method is called on the DbContext
?
相关问题
- Do I need to close the DbConnection manually when
- Foreign key with composite key in EF Core
- List returned by Entity Framework is null [duplica
- EF Core - Return mapped Many-to-Many relationship
- How to I access the DbContext of EF core from anot
相关文章
- EF Core 'another instance is already being tra
- many to many EF7
- ASP.NET Core DbContext injection
- IDbAsyncQueryProvider in EntityFrameworkCore
- Entity Framework 7 DbContext scaffold
- Execute SQL command in Entity Framework Core 2.0 t
- Modelling folder structure in Entity Framework Cor
- EF Core 2.1 - Duplicate relationships when use flu
you can use console logger "EF Core logging automatically integrates with the logging mechanisms of .NET Core " you can read about here : https://www.entityframeworktutorial.net/efcore/logging-in-entityframework-core.aspx
You can use
DbContextOptionsBuilder.UseLoggerFactory(loggerFactory)
method to log all sql output.By using constructor Injection like belowOr
I suggest a few other ways of viewing the SQL generated is to use reflection to create an
ObjectQuery object
and then call theToTraceString()
method to actually store the query results.Use SQL Logging
Using The
DbContext.Database.Log property
can be set to a delegate for any method that takes a string.Log SQL to the Console.
Log SQL to Visual Studio Output panel.
Here are the docs on creating a LoggerFactory in Core 3. In short:
You may need to add a reference to
Microsoft.Extensions.Logging.Console
.Use the
DbContextOptionsBuilder
to enable logging for a context.I'll repeat the warning from here:
Therefore, they recommend using a singleton/global instance: