首页 > 数据库技术 > 详细

Linq模型ObjectContext下查看Sql语句。

时间:2014-05-26 11:24:09      阅读:445      评论:0      收藏:0      [点我收藏+]

ObjectContext 并没有提供 LINQ to SQL DataContext.Log 这样的功能,要查看实际生成的 T-SQL 语句,要么借助 SQL Server Sql Profiler 这样的工具,要么使用 ObjectQuery.ToTraceString() 方法。

using (var context = new TestEntities())

{
    var users1 = context.User.Where(u => u.Name == "user1");
    Console.WriteLine((users1 as ObjectQuery).ToTraceString());
var users2 = from u in context.User where u.Name == "user1" select u; Console.WriteLine((users2 as ObjectQuery).ToTraceString());

var sql = "SELECT VALUE u FROM TestEntities.User AS u WHERE u.Name = ‘user1‘"; var users3 = context.CreateQuery<User>(sql); Console.WriteLine(users3.ToTraceString()) }

输出:

SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Age] AS [Age]
FROM [dbo].[User] AS [Extent1]
WHERE N‘user1‘ = [Extent1].[Name]
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Age] AS [Age]
FROM [dbo].[User] AS [Extent1]
WHERE N‘user1‘ = [Extent1].[Name]
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Age] AS [Age]
FROM [dbo].[User] AS [Extent1]
WHERE [Extent1].[Name] = ‘user1‘

 

Linq模型ObjectContext下查看Sql语句。,布布扣,bubuko.com

Linq模型ObjectContext下查看Sql语句。

原文:http://www.cnblogs.com/wgx0428/p/3747014.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!