In development, when using the ef core framework, when using linq or lambda expressions, we cannot directly view the sql statements, which is inconvenient for us to troubleshoot or optimize performance.
This article uses EF Core 5 as an exampleto view the SQL statements generated by the output EF.
First, create a new consolelogger factory variable in the startup file with the variable "ConsoleLoggerFactory", and the generated sql statement will be output to the console, the code is as follows:
Add log output to the ConfigureServices method by configuring the DbContext code as follows:
Start the project and see the console output as follows:
info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [a]. [Id], [a]. [Disabled], [a]. [Link], [a]. [Name], [a]. [PinYin], [a]. [Position], [a]. [_CreateTime], [a]. [_CreateTimeStamp], [a]. [_UpdateTime], [a]. [_UpdateTimeStamp] FROM [Area] AS [a]
info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (19ms) [Parameters=[p0='?' (Size = 4000)], CommandType='Text', CommandTimeout='30'] SELECT * FROM Area WHERE pinYin = @p0
info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT * FROM Area
If the SQL statement with the where condition can only view the parameterized variables, not the specific values passed, the following configuration is added:
Enable application data to be included in exception messages, logs, and more.
This can include values assigned to entity instance properties,
parameter values of commands sent to the database, and other such data.
This flag should only be enabled if there are safety measures in place
Based on the sensitivity of this data.
Note if the application is going through
Call the Microsoft.EntityFrameworkCore.DbContextOptions generator.UseInternalServiceProvider(system. IServiceProvider),
This option must then be configured in the same way for all usage of that service provider.
Consider not calling Microsoft.EntityFrameworkCore.DbContextOptionsGenerator.UseInternalServiceProvider(system. IServiceProvider)
EF will therefore manage the service provider and can create new instances as needed
The full setup is as follows:
The renderings are as follows:
Of course, the above settings can also be configured by overriding the OnConfiguring method of the DbContext (not recommended):
(End)
|