1、在YourDbContext构造函数中配置Database.Log。
1 public partial class XxxEntities: DbContext 2 { 3 public XxxEntities() 4 : base("name=XxxEntities") 5 { 6 System.Diagnostics.Debug.WriteLine("--实例化DbContext--"); 7 Database.Log = x => { System.Diagnostics.Debug.WriteLine(x); }; 8 } 9 }
2、在Debug(F5)环境下测试,才能在output窗口中输出SQL信息。
1 --实例化DbContext-- 2 已于 2015/6/2 12:51:38 +08:00 3 打开了连接 4 SELECT 5 [Extent1].[CoSetID] AS [CoSetID], 6 [Extent1].[BeginTime] AS [BeginTime], 7 [Extent1].[EndTime] AS [EndTime], 8 [Extent1].[CreateTime] AS [CreateTime], 9 [Extent1].[CreatorID] AS [CreatorID], 10 [Extent1].[Creator] AS [Creator], 11 [Extent1].[LastUpdateTime] AS [LastUpdateTime], 12 [Extent1].[LastEditorID] AS [LastEditorID], 13 [Extent1].[LastEditor] AS [LastEditor], 14 [Extent1].[IsEnabled] AS [IsEnabled], 15 [Extent1].[IsDeleted] AS [IsDeleted], 16 [Extent1].[Timestamp] AS [Timestamp] 17 FROM [dbo].[CourseSet] AS [Extent1] 18 WHERE 0 = [Extent1].[IsDeleted] 19 ORDER BY [Extent1].[BeginTime] ASC 20 21 22 -- 正在 2015/6/2 12:51:39 +08:00 23 执行 24 -- 已在 25 毫秒内完成,结果为: SqlDataReader 25 26 27 28 已于 2015/6/2 12:51:39 +08:00 29 关闭了连接
默认情况下,VS会输出许多其它类型的消息,但是可以在output窗口中右击选择需要输出的消息类型。