Steve Johnstone
Steve Johnstone

Reputation: 576

Profiling Entity Framework With MvcMiniProfiler

I've got an Asp.net Mvc 3 Application which is now using the MvcMiniProfiler. I'm also using the Entity Framework to access my database, and I'd like to enable the profiler to work with the entity model. So far I've created the Context factory below:

internal class ProfiledContextFactory : IContextFactory
{
    public ModelContainer GetContext()
    {
        var conn = ProfiledDbConnection.Get(GetConnection());
        return ObjectContextUtils.CreateObjectContext<ModelContainer>(conn);
    }

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

When I run the above code, which is called by my repository layer when I start a unit of work, it gets stuck in an infite loop when calling CreateDbCommandDefinition in the MvcMiniProfiler.ProfiledDbServices class.

Any clues what I'm doing wrong?

Upvotes: 3

Views: 676

Answers (1)

Steve Johnstone
Steve Johnstone

Reputation: 576

The problem was my GetConnection was returning the EntityConnection, not the SqlConnection within the EntityConnection. I've now modified my code so that it reads:

private static SqlConnection GetConnection()
{
    var connStr = ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString;
    var entityConnStr = new EntityConnectionStringBuilder(connStr);
    return new SqlConnection(entityConnStr.ProviderConnectionString);
}

And it works fine.

I discovered this while looking at this question: Using mvc-mini-profiler with EF 4.0 and Ninject

Upvotes: 3

Related Questions