简体   繁体   English

将LINQ生成的SQL记录到.net中的Entity Framework中的SQL

[英]Logging the SQL generated by LINQ to SQL in Entity Framework in .net

I am designing a testing framework that makes extensive use of SQL Sever Database. 我正在设计一个广泛使用SQL Sever数据库的测试框架。 I am using Entity Framework 6 of .NET to felicitate it. 我正在使用 .NET的Entity Framework 6来祝贺它。 I want to log the Underlying SQL query each time when I run a test case. 我想在每次运行测试用例时记录底层SQL查询。 I am using LINQ to SQL for querying Database. 我正在使用LINQ to SQL来查询数据库。

I am having a hard time logging the SQL. 我很难记录SQL。 LINQ to SQL generates some uncooked SQL which needs to be converted into SQL by filling in the parameters which I want to avoid. LINQ to SQL生成一些未经烹饪的SQL,需要通过填写我想要避免的参数将其转换为SQL。

Is there a better approach which will log all the SQL which I can directly feed to my SQL Server without doing any changes in Query ? 有没有更好的方法可以记录所有SQL,我可以直接提供给我的SQL Server而无需在Query中进行任何更改?

According to Entity Framework Logging : 根据实体框架记录

The DbContext.Database.Log property can be set to a delegate for any method that takes a string. 对于采用字符串的任何方法,可以将DbContext.Database.Log属性设置为委托。 Most commonly it is used with any TextWriter by setting it to the “Write” method of that TextWriter. 最常见的是,它通过将其设置为TextWriter的“Write”方法与任何TextWriter一起使用。 All SQL generated by the current context will be logged to that writer. 当前上下文生成的所有SQL都将记录到该编写器。 For example, the following code will log SQL to the console: 例如,以下代码将SQL记录到控制台:

 using (var context = new BlogContext()) { context.Database.Log = Console.Write; // Your code here... } 

in the above way you should be able to log everything. 以上述方式,您应该能够记录所有内容。

The following gets logged: 记录以下内容:

When the Log property is set all of the following will be logged: 设置Log属性时,将记录以下所有内容:

  • SQL for all different kinds of commands. 用于所有不同类型命令的SQL。 For example: 例如:
    • Queries, including normal LINQ queries, eSQL queries, and raw queries from methods such as SqlQuery 查询,包括正常的LINQ查询,eSQL查询和来自SqlQuery等方法的原始查询
    • Inserts, updates, and deletes generated as part of SaveChanges 作为SaveChanges的一部分生成的插入,更新和删除
    • Relationship loading queries such as those generated by lazy loading 关系加载查询,例如延迟加载生成的查询
  • Parameters 参数
  • Whether or not the command is being executed asynchronously 是否异步执行命令
  • A timestamp indicating when the command started executing 一个时间戳,指示命令何时开始执行
  • Whether or not the command completed successfully, failed by throwing an exception, or, for async, was canceled 命令是否成功完成,抛出异常失败,或异步,是否已取消
  • Some indication of the result value 结果值的一些指示
  • The approximate amount of time it took to execute the command. 执行命令所花费的大致时间。 Note that this is the time from sending the command to getting the result object back. 请注意,这是从发送命令到返回结果对象的时间。 It does not include time to read the results. 它没有时间来阅读结果。

Looking at the example output above, each of the four commands logged are: 查看上面的示例输出,记录的四个命令中的每一个都是:

  • The query resulting from the call to context.Blogs.First 调用context.Blogs.First产生的查询
    • Notice that the ToString method of getting the SQL would not have worked for this query since “First” does not provide an IQueryable on which ToString could be called 请注意,获取SQL的ToString方法不适用于此查询,因为“First”不提供可以调用ToString的IQueryable
  • The query resulting from the lazy-loading of blog.Posts 由于延迟加载blog.Posts而产生的查询
    • Notice the parameter details for the key value for which lazy loading is happening 请注意延迟加载发生的键值的参数详细信息
    • Only properties of the parameter that are set to non-default values are logged. 仅记录设置为非默认值的参数属性。 For example, the Size property is only shown if it is non-zero. 例如,只有在非零时才显示Size属性。
  • Two commands resulting from SaveChangesAsync; 由SaveChangesAsync产生的两个命令; one for the update to change a post title, the other for an insert to add a new post 一个用于更新以更改帖子标题,另一个用于插入以添加新帖子
    • Notice the parameter details for the FK and Title properties 请注意FK和Title属性的参数详细信息
    • Notice that these commands are being executed asynchronously 请注意,这些命令是异步执行的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM