获取EF查询的SQL语句

在EF编程中我们可以通过lamda表达式可以进行查询数据,获取到IQueryable结果,我们要想知道具体的SQL语句是什么需要使用ObjectQuery<T>进行处理

处理方法如下:

            IQueryable data = this.ObjectContext.Data.Where(d => d.Name.StartsWith("测试"));
            ObjectQuery oq=data as ObjectQuery;
            String sql=oq.ToTraceString();

运行结果如下:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name]
FROM (SELECT 
      [Data].[ID] AS [ID],    
      [Data].[Name] AS [Name]
      FROM [dbo].[Data] AS [Data]) AS [Extent1]
WHERE [Extent1].[Name] LIKE '测试%'


你可能感兴趣的:(C#,EntityFramework)