Using mvc-mini-profiler with EF 4.0 and Ninject

前端 未结 3 813
有刺的猬
有刺的猬 2021-02-05 23:22

I\'m trying to use the new mvc-mini-profiler with my EF4 based app, but I have no idea how to properly get a connection to my destination datasource.

Here\'s as far as I

3条回答
  •  既然无缘
    2021-02-05 23:45

    Here is a slightly better performing, but slightly hackier solution to getting the store connection.

        public static DbConnection GetStoreConnection() where T : System.Data.Objects.ObjectContext
        {
            return GetStoreConnection("name=" + typeof(T).Name);
        }
    
        public static DbConnection GetStoreConnection(string entityConnectionString)
        {
            DbConnection storeConnection;
    
            // Let entity framework do the heavy-lifting to create the connection.
            using (var connection = new EntityConnection(entityConnectionString))
            {
                // Steal the connection that EF created.
                storeConnection = connection.StoreConnection;
    
                // Make EF forget about the connection that we stole (HACK!)
                connection.GetType().GetField("_storeConnection",
                    BindingFlags.NonPublic | BindingFlags.Instance).SetValue(connection, null);
    
                // Return our shiny, new connection.
                return storeConnection;
            }
        }
    

提交回复
热议问题