Recursive error when using Entity Framework. Create


Edited: The solution to this problem

The solution to this problem is simple, only one line of code is required to profile entity framework connections, namely this: MiniProfilerEF.Initialize();

No other custom connection creation or any other complicated code is needed at all.

As soon as my first query executes the following code get’s into an infinite loop.

protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, System.Data.Common.CommandTrees.DbCommandTree commandTree)
    var cmdDef = wrapped.CreateCommandDefinition(providerManifest, commandTree);
    var cmd = cmdDef.CreateCommand();
    return CreateCommandDefinition(new ProfiledDbCommand(cmd, cmd.Connection, profiler));

CreateCommandDefinition causes a stack overflow.

I initialise the connection like this.

const string Format = "metadata=res://*/SqlEntities.CacheEntities.csdl|res://*/SqlEntities.CacheEntities.ssdl|res://*/SqlEntities.CacheEntities.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"";

public static DocumentEntities CreateContainer() {
    var connectionString = string.Format(Format, Db.GetConnection(Datastore.DocumentManagement));
    var entityConnection = new EntityConnection(connectionString);
    var dataConnection = new StackExchange.Profiling.Data.EFProfiledDbConnection(entityConnection, MiniProfiler.Current);
    return dataConnection.CreateObjectContext<DocumentEntities>();

It seems to occur when in EFProfiledDbProviderFactory a new ProfiledDbProviderServices is created

Any Ideas?

I have (after much reading and investigating) discovered that I was doing the wrong thing completely.

The only line of code required is this

No changes to the connection creation or any other such code is required.