当前位置: 首页 > 面试题库 >

从EntityFramework中查看具有类集合查询的SQL

何楷
2023-03-14
问题内容

我没有MS SQL(SQL Express 2008)的“完整”版本,所以我没有探查器工具。

我想查看由我的实体框架代码生成的SQL,但是我发现的所有示例都使用了

var x = from u in table
        select u;

语法类型;但我的大部分查询更像是..

var x = context.Users.Single(n => n.Name == "Steven");

语法类型。如何查看通过这种编码方式生成的SQL?有任何想法吗?


问题答案:

Express Edition是否支持扩展事件?如果是这样,它将以类似于Profiler的方式捕获语句并sp完成事件。

编辑:
我已将其更改为使用内存目标,而不是文件目标。理想情况下,取消注释这些WHERE部分,并用适当的用户名替换以仅捕获感兴趣的事件,或者您可以使用spid进行筛选WHERE (([sqlserver].[session_id]=(56))),例如。

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='test_trace')
    DROP EVENT SESSION [test_trace] ON SERVER;
CREATE EVENT SESSION [test_trace]
ON SERVER
ADD EVENT sqlserver.sp_statement_completed(
     ACTION (package0.callstack, sqlserver.session_id, sqlserver.sql_text)
    -- WHERE (([sqlserver].[username]='Domain\Username'))
    ),
ADD EVENT sqlserver.sql_statement_completed(
     ACTION (package0.callstack, sqlserver.session_id, sqlserver.sql_text)
     --WHERE (([sqlserver].[username]='Domain\Username'))
     )
ADD TARGET package0.ring_buffer
WITH (MAX_MEMORY = 4096KB, EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS, 
MAX_DISPATCH_LATENCY = 1 SECONDS, MAX_EVENT_SIZE = 0KB, 
MEMORY_PARTITION_MODE = NONE, TRACK_CAUSALITY = OFF, STARTUP_STATE = OFF)

ALTER EVENT SESSION [test_trace] ON SERVER STATE = START

并查看结果(使用Adam
Machanic的XE代码生成器生成的查询

DECLARE 
    @session_name VARCHAR(200) = 'test_trace'

SELECT 
    pivoted_data.* 
FROM 
( 
 SELECT MIN(event_name) AS event_name,
     MIN(event_timestamp) AS event_timestamp,
     unique_event_id,
     CONVERT ( BIGINT, MIN (
         CASE
             WHEN d_name = 'cpu'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [cpu],
     CONVERT ( BIGINT, MIN (
         CASE
             WHEN d_name = 'duration'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [duration],
     CONVERT ( BIGINT, MIN (
         CASE
             WHEN d_name = 'object_id'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [object_id],
     CONVERT ( INT, MIN (
         CASE
             WHEN d_name = 'object_type'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [object_type],
     CONVERT ( DECIMAL(28,0), MIN (
         CASE
             WHEN d_name = 'reads'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [reads],
     CONVERT ( VARCHAR(MAX), MIN (
         CASE
             WHEN d_name = 'session_id'
             AND d_package IS NOT NULL
             THEN d_value
         END ) ) AS [session_id],
     CONVERT ( INT, MIN (
         CASE
             WHEN d_name = 'source_database_id'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [source_database_id],
     CAST((SELECT CONVERT ( VARCHAR(MAX), MIN (
         CASE
             WHEN d_name = 'sql_text'
             AND d_package IS NOT NULL
             THEN d_value
         END ) )  AS [processing-instruction(x)] FOR XML PATH('') ) AS XML) AS [sql_text],
     CONVERT ( DECIMAL(28,0), MIN (
         CASE
             WHEN d_name = 'writes'
             AND d_package IS NULL
             THEN d_value
         END ) ) AS [writes]
 FROM
    ( 
        SELECT 
            *, 
            CONVERT(VARCHAR(400), NULL) AS attach_activity_id 
        FROM 
        ( 
            SELECT 
                event.value('(@name)[1]', 'VARCHAR(400)') as event_name, 
                event.value('(@timestamp)[1]', 'DATETIME') as event_timestamp, 
                DENSE_RANK() OVER (ORDER BY event) AS unique_event_id, 
                n.value('(@name)[1]', 'VARCHAR(400)') AS d_name, 
                n.value('(@package)[1]', 'VARCHAR(400)') AS d_package, 
                n.value('((value)[1]/text())[1]', 'VARCHAR(MAX)') AS d_value, 
                n.value('((text)[1]/text())[1]', 'VARCHAR(MAX)') AS d_text 
            FROM 
            ( 
                SELECT 
                    ( 
                        SELECT 
                            CONVERT(xml, target_data) 
                        FROM sys.dm_xe_session_targets st 
                        JOIN sys.dm_xe_sessions s ON 
                            s.address = st.event_session_address 
                        WHERE 
                            s.name = @session_name 
                            AND st.target_name = 'ring_buffer' 
                    ) AS [x] 
                FOR XML PATH(''), TYPE 
            ) AS the_xml(x) 
            CROSS APPLY x.nodes('//event') e (event) 
            CROSS APPLY event.nodes('*') AS q (n) 
        ) AS data_data 
    ) AS activity_data 
    GROUP BY 
        unique_event_id 
) AS pivoted_data;


 类似资料:
  • 问题内容: 有没有办法观察将由生成的SQL语句? 例如,我有这个:是否 可以查看其基础的原始SQL? 问题答案: 光滑2.X: 您可以按照Slick文档中所示打印查询语句: 对于其他类型的报表看,和。 光滑的3.X: 文件。

  • 2-查询id为1的人的所有地址,2我想: 好的方法是什么?

  • scard key 如果set是空或者key不存在返回0

  • 在Firestore中,我有一个用户集合,在每个用户文档中存储一个名为favorites的集合,该集合包含标记为favorites(存储)的文档的ID 我可以采取另一种方法来获取两个集合并手动过滤它们,但我使用的是Firebase RecycerView适配器,它显示的所有数据都基于查询,这将使事情更加高效。 这样的结果是如何实现的呢?如果需要进一步的解释,请告诉我

  • 问题内容: 我正在写一个JPQL查询,它基于 Categories 集合进行查询。我的类别可以为空,因此我正在使用:categories = NULL进行检查。 当类别为NULL时,以上方法可以正常工作。但是,当类别超过一个值时,我得到了错误 java.sql.SQLException:操作数应包含1列 与hibernate有关的痕迹是 (?,?)或(?,?)中的category6_.catego

  • 我有一个实体,包含s的嵌入式集合。 我想找到集合中与给定参数(字符串)匹配的所有s。 我想在JPQL中实现的一些伪代码: 但是,当我尝试此查询时: 它抛出: 我希望避免使用需要连接多个表的解决方案。这是伪代码,但实际查询中充满了大量复杂的大型企业sql where或and语句。所以越少加入越好。