lulisheng0007 / mvc-mini-profiler

Automatically exported from code.google.com/p/mvc-mini-profiler
0 stars 0 forks source link

Provide an easier way to use the library with EF. #12

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Creating a DbConnection for an EF 4.0 context is a bit of a hacky process that 
is hard to get right.

The `ObjectContextUtils.CreateObjectContext<T>()` method should provide an 
overload that use reflection (on T) and pulls the connection string from the 
configuration manager.

This would lower the barrier to entry, and make code that is using the profiler 
easier to read and maintain.

Original issue reported on code.google.com by Spoonful...@gmail.com on 9 Jun 2011 at 8:45

GoogleCodeExporter commented 8 years ago
Just as a comment this would be equally nice for use with EF 4.1.  As you don't 
have to specify what DataBase provider you are using in code as it is read from 
the connection string which is named after the class by convention.

Original comment by B...@marastat.com on 9 Jun 2011 at 9:21

GoogleCodeExporter commented 8 years ago
As an alternative, the `EntityConnection` class can take a connection string 
formatted like `string.Format("name={0}", typeof(T).Name)`

After that, there is a `StoreConnection` property on the EntityConnection class 
that could be commandeered (and set null via reflection).

http://stackoverflow.com/questions/6296444/using-mvc-mini-profiler-with-ef-4-0-a
nd-ninject/6299635#6299635

Of course, this is a pretty hacky solution, but it works quite well.

Original comment by Spoonful...@gmail.com on 9 Jun 2011 at 10:43

GoogleCodeExporter commented 8 years ago

Original comment by nrcraver on 20 Jun 2011 at 1:04

GoogleCodeExporter commented 8 years ago
Here is something that I think covers most of the edge cases, and doesn't rely 
on reflection hacks:

        public static DbConnection GetStoreConnection<T>() where T : System.Data.Objects.ObjectContext
        {
            return GetStoreConnection("name=" + typeof(T).Name);
        }

        public static DbConnection GetStoreConnection(string entityConnectionString)
        {
            // Build the initial connection string.
            var builder = new EntityConnectionStringBuilder(entityConnectionString);

            // If the initial connection string refers to an entry in the configuration, load that as the builder.
            object configName;
            if (builder.TryGetValue("name", out configName))
            {
                var configEntry = WebConfigurationManager.ConnectionStrings[configName.ToString()];
                builder = new EntityConnectionStringBuilder(configEntry.ConnectionString);
            }

            // Find the proper factory for the underlying connection.
            var factory = DbProviderFactories.GetFactory(builder.Provider);

            // Build the new connection.
            DbConnection tempConnection = null;
            try
            {
                tempConnection = factory.CreateConnection();
                tempConnection.ConnectionString = builder.ProviderConnectionString;

                var connection = tempConnection;
                tempConnection = null;
                return connection;
            }
            finally
            {
                // If creating of the connection failed, dispose the connection.
                if (tempConnection != null)
                {
                    tempConnection.Dispose();
                }
            }
        }

Original comment by Spoonful...@gmail.com on 27 Jun 2011 at 8:29

GoogleCodeExporter commented 8 years ago
I will put that into a commit if you want to use it.  Just tell me where you 
want it to go.

Original comment by Spoonful...@gmail.com on 27 Jun 2011 at 8:29

GoogleCodeExporter commented 8 years ago
@Spoonful we confess that we are not EF experts, hence why we're behind on 
this. If you're familiar with HG, feel free to clone and push us the change 
(and ping us here so we see it).

Original comment by marc.gravell on 27 Jun 2011 at 9:07

GoogleCodeExporter commented 8 years ago
just pulled this in ... we now have 2 methods to profile EF. 

Will be happy if we can deprecate the old way .. need guidance from the EF team 

Original comment by sam.saff...@gmail.com on 15 Aug 2011 at 1:05