2012-06-26 15 views
4

我在我的項目中通過nuget安裝了MiniProfiler和MiniProfiler.EF。無法設置MiniProfiler W/Enity Framework 4.0(不代碼優先)

使用MiniProfiler之前,我會在我的模型庫中使用該打開的連接:

public class NotificationRepository 
    { 
     private CBNotificationModel.CB_NotificationEntities db; 

     public NotificationRepository() 
     { 
      db = new CB_NotificationEntities(); 
     } 

     public NotificationContact GetNotificationContacts() 
     { 
      return db.NotificationContacts.ToList(); 
     } 
    } 

要使用我創建的迷你探查:

public static class ConnectionHelper 
    { 
     public static CB_NotificationEntities GetEntityConnection() 
     { 
      var conn = new StackExchange.Profiling.Data.EFProfiledDbConnection(GetConnection(), MiniProfiler.Current); 

      return ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(conn); // resides in the MiniProfiler.EF nuget pack 
     } 

     public static EntityConnection GetConnection() 
     { 
      return new EntityConnection(ConfigurationManager.ConnectionStrings["CB_NotificationEntities"].ConnectionString); 
     } 
    } 

模型庫現在使用

db = ConnectionHelper.GetEntityConnection(); 

但是,這給出了錯誤:

mscorlib.dll中發生未處理的異常類型'System.StackOverflowException'

我是否錯過了一個步驟?我嘗試在Application_start()中添加MiniProfilerEF.Initialize()和MiniProfilerEF.Initialize_EF42(),但它只是改變了給出的錯誤。

似乎沒有太多的信息來設置實體框架項目使用miniprofiler,除非它是codefirst。

回答

7

我能夠改變我的ConnectionHelper級以下得到這個工作:

public static class ConnectionHelper 
    { 
      public static CB_NotificationEntities GetEntityConnection() 
      { 

       var connectionString = ConfigurationManager.ConnectionStrings["CB_NotificationEntities"].ConnectionString; 
       var ecsb = new EntityConnectionStringBuilder(connectionString); 
       var sqlConn = new SqlConnection(ecsb.ProviderConnectionString); 
       var pConn = new StackExchange.Profiling.Data.EFProfiledDbConnection(sqlConn, MiniProfiler.Current); 

       var context = ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(pConn); 
       return context; 

      } 
    } 
+1

我希望我可以給你這個這麼多點,我看遍這 –

+0

哦,是的,謝謝。它是我第二次嘗試使MiniProfiler工作,每次我得到一個StackOverflowException。此解決方案完美運作。 –