【小菜日志】用LINQ的DataContext.Log 属性来记录LINQ TO语句操作日志
大家知道,平时我们追踪SQL语句的执行时都可以用SQLProfiler,不过使用其他数据库操作就不得而知了
其实针对LINQ TO MORE 我们可以使用LINQ的DataContext.Log属性来查看或者记录LINQ TO 数据库的操作日志
(貌似只能针对可以实例化的数据库连接类型,像LINQ TO XML这种除外),很菜很简单的东西,所以直接贴代码了
1 public ActionResult View(int? Id)
2 {
3 System.IO.TextWriter log;
4 string path = @"c:\TestLinqSQL.sql";
5 log = System.IO.File.AppendText(path);
6 using (WebSiteDataBaseDataContext db = new WebSiteDataBaseDataContext())
7 {
8 //db.Log = Console.Out;
9 db.Log = log;
10 log.WriteLine("命令1");
11 News NS = db.News.First(n => n.NewsId.Equals(Id));
12 NS.Hits = NS.Hits + 1;
13 log.WriteLine("命令2");
14 var _NewsView = db.News.Single(n => n.NewsId.Equals(Id));
15 log.Close();
16 ViewData["_NV"] = _NewsView;
17 ViewData["_TL"] = MB.GetAllTypeList(1);
18 return View();
19 }
20 }
2 {
3 System.IO.TextWriter log;
4 string path = @"c:\TestLinqSQL.sql";
5 log = System.IO.File.AppendText(path);
6 using (WebSiteDataBaseDataContext db = new WebSiteDataBaseDataContext())
7 {
8 //db.Log = Console.Out;
9 db.Log = log;
10 log.WriteLine("命令1");
11 News NS = db.News.First(n => n.NewsId.Equals(Id));
12 NS.Hits = NS.Hits + 1;
13 log.WriteLine("命令2");
14 var _NewsView = db.News.Single(n => n.NewsId.Equals(Id));
15 log.Close();
16 ViewData["_NV"] = _NewsView;
17 ViewData["_TL"] = MB.GetAllTypeList(1);
18 return View();
19 }
20 }
命令1
SELECT TOP (1) [t0].[NewsId], [t0].[TitleZ], [t0].[TitleF], [t0].[Pics], [t0].[Contents], [t0].[Times], [t0].[Summary], [t0].[Author], [t0].[Infor], [t0].[Orders], [t0].[ClassName], [t0].[Hits], [t0].[Levels], [t0].[Tags], [t0].[IsTop], [t0].[IsTitle], [t0].[TitleUrl], [t0].[Templete], [t0].[Good], [t0].[Bad], [t0].[State], [t0].[Review], [t0].[Color]
FROM [dbo].[News] AS [t0]
WHERE [t0].[NewsId] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [51]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
命令2
SELECT [t0].[NewsId], [t0].[TitleZ], [t0].[TitleF], [t0].[Pics], [t0].[Contents], [t0].[Times], [t0].[Summary], [t0].[Author], [t0].[Infor], [t0].[Orders], [t0].[ClassName], [t0].[Hits], [t0].[Levels], [t0].[Tags], [t0].[IsTop], [t0].[IsTitle], [t0].[TitleUrl], [t0].[Templete], [t0].[Good], [t0].[Bad], [t0].[State], [t0].[Review], [t0].[Color]
FROM [dbo].[News] AS [t0]
WHERE [t0].[NewsId] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [51]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
}
SELECT TOP (1) [t0].[NewsId], [t0].[TitleZ], [t0].[TitleF], [t0].[Pics], [t0].[Contents], [t0].[Times], [t0].[Summary], [t0].[Author], [t0].[Infor], [t0].[Orders], [t0].[ClassName], [t0].[Hits], [t0].[Levels], [t0].[Tags], [t0].[IsTop], [t0].[IsTitle], [t0].[TitleUrl], [t0].[Templete], [t0].[Good], [t0].[Bad], [t0].[State], [t0].[Review], [t0].[Color]
FROM [dbo].[News] AS [t0]
WHERE [t0].[NewsId] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [51]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
命令2
SELECT [t0].[NewsId], [t0].[TitleZ], [t0].[TitleF], [t0].[Pics], [t0].[Contents], [t0].[Times], [t0].[Summary], [t0].[Author], [t0].[Infor], [t0].[Orders], [t0].[ClassName], [t0].[Hits], [t0].[Levels], [t0].[Tags], [t0].[IsTop], [t0].[IsTitle], [t0].[TitleUrl], [t0].[Templete], [t0].[Good], [t0].[Bad], [t0].[State], [t0].[Review], [t0].[Color]
FROM [dbo].[News] AS [t0]
WHERE [t0].[NewsId] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [51]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
}
个人很觉得开发轻量级项目使用LINQ TO SQL相当方便,希望高手们不要鄙视我
其实在国外已经有不少LINQ TO MORE的成功案例了,包括一些中型的视频门户网站
另外我觉得学习LINQ TO MORE也可以帮助我们了解C#的新特性,或者可以轻松转到.Net EF,同时学习lambda表达式也是不错的