# Recursive error when using Entity Framework. Create

**URL:** <https://community.miniprofiler.com/t/recursive-error-when-using-entity-framework-create/50>\
**Category:** uncategorized\
**Created:** [October 30, 2012, 2:02am UTC](https://community.miniprofiler.com/t/recursive-error-when-using-entity-framework-create/50 "2012-10-30T02:02:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Unknown26](https://sea2.discourse-cdn.com/flex016/user_avatar/community.miniprofiler.com/unknown26/32/1003_2.png) [@Unknown26](https://community.miniprofiler.com/u/Unknown26)\
**Post date:** [October 30, 2012, 2:02am UTC](https://community.miniprofiler.com/t/recursive-error-when-using-entity-framework-create/50/1 "2012-10-30T02:02:46Z")

</div>

Hi,

* * *

**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?

---

<div class="post-metadata">

**Author:** ![Unknown26](https://sea2.discourse-cdn.com/flex016/user_avatar/community.miniprofiler.com/unknown26/32/1003_2.png) [@Unknown26](https://community.miniprofiler.com/u/Unknown26)\
**Post date:** [November 4, 2012, 8:20pm UTC](https://community.miniprofiler.com/t/recursive-error-when-using-entity-framework-create/50/2 "2012-11-04T20:20:36Z")

</div>

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

The only line of code required is this  
MiniProfilerEF.Initialize();

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