2011-06-09 50 views
18

ASP.NET MVC Mini Profiler看起來很棒,但我沒有得到Linq 2 SQL的使用示例。如何使ASP.NET MVC迷你探查器與Linq 2 SQL一起工作?

這是從探查文檔LINQ2SQL例如:

partial class DBContext 
{ 
    public static DBContext Get() 
    { 
     var conn = ProfiledDbConnection.Get(GetConnection()); 
     return new DBContext(conn); 
     // or: return DataContextUtils.CreateDataContext<DBContext>(conn); 
    } 
} 

如何我在實際應用中使用?我會期望在我的DataContext中使用某種包裝,但是這似乎以不同的方式工作。我甚至不知道該例中的「GetConnection()」方法在哪裏定義。

感謝,

阿德里安

回答

7

終於搞明白了。如果其他人有相同的問題:

private static DataClassesDataContext CreateNewContext() 
     { 
      var sqlConnection = new SqlConnection(<myconnectionstring>); 
      var profiledConnection = ProfiledDbConnection.Get(sqlConnection); 
      return DataContextUtils.CreateDataContext<DataClassesDataContext>(profiledConnection); 

     } 
+4

整點是,它是在安全生產中使用,有沒有需要做的#if DEBUG東西......如果會話不分析獲取將返回原始連接 – 2011-06-09 23:30:20

+0

即時通訊使用迷你分析器與LINQ到SQL PLZ看看http://stackoverflow.com/questions/6410756/using-different-overload-of-datacontext- in-linq-to-sql – 2011-06-21 06:09:18

+0

你把這個方法放在哪裏?你打算如何調用它? – RyanW 2011-08-26 15:53:29

3

的getConnection()是將返回的DbConnection的功能。你可能只是做

var conn = ProfiledDbConnection.Get(new System.Data.SqlClient.SqlConnection(your_connection_string)); 

改爲。

6

沒有其他答案爲我工作。在我DataClasses.Designer.cs添加此我DataClassesDataContext階級都:

public static DataClassesDataContext CreateNewContext() 
{ 
    var sqlConnection = new DataClassesDataContext().Connection; 
    var profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(sqlConnection); 
    return new DataClassesDataContext(profiledConnection); 
} 
+3

似乎不適用於我,ProfiledDConnection上沒有定義Get()方法。 – RyanW 2011-08-26 16:07:47

+2

@RyanW'改爲使用新的ProfiledDbConnection(sqlConnection,MiniProfiler.Current)'。 – 2012-07-13 11:26:22

相關問題