This article is a mirror article of machine translation, please click here to jump to the original article.

View: 55374|Reply: 2

[Source] EF Core Series 4 looks at the generated SQL statements

[Copy link]
Posted on 2/19/2021 1:36:34 PM | | | |
EF Core Series 2 encapsulates OnModelCreating, creates indexes using reflections, and more
https://www.itsvse.com/thread-9392-1-1.html

EF Core Series (3) Entity Frame Shadow Attributes [Reprint]
https://www.itsvse.com/thread-9558-1-1.html


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.
ef displays the generated SQL statements
https://www.itsvse.com/thread-3813-1-1.html

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)




Previous:Financial TipRanks Stock Analysis Ratings
Next:EF Core Series (5) executes SQL statements or views, stored procedures
 Landlord| Posted on 9/26/2021 9:21:07 AM |
ef displays the generated SQL statements
https://www.itsvse.com/thread-3813-1-1.html
Posted on 10/26/2021 9:46:19 AM |
Study, see how the time limit is
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com