k-dev
k-dev

Reputation: 1657

mvc miniprofiler uses DbConnection and entity framework EntityConnection

I feel I am missing something really simple here, I am integrating MVC Miniprofiler into my project but I can't find the way to instantiate my context with the right connection, because it is not EF Code First and my model inherits from ObjectContext, here is my code

public static MyModel GetDataBaseModel()
{       
    DbConnection conn = new MySqlConnection(Utils.GetConnectionString());

    // this returns a DBConnection
    DbConnection profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(conn);  

    //my model needs an EntityConnection, so this is not possible
    return new MyModel(profiledConnection); 
}

How to solve this?

Upvotes: 1

Views: 713

Answers (1)

J. Tihon
J. Tihon

Reputation: 4459

I assume that MyModel is the context created by the Entity Framework Designer, right? An EntityConnection can be created from a DbConnection in combination with a MetadataWorkspace. I also assume that the EF Designer has added a connectionstring-property to your app/web.config which contains something like this:

metadata=res://*/Content.MyModel.csdl|res://*/Content.MyModel.ssdl|res://*/Content.MyModel.msl;

These are the 3 components that make up your MetadataWorkspace. To create your an EntityConnection from these information you must provide each file within an array:

string[] paths =
{
    // "res://" refers to a embedded resource within your DLL, this is automatically done if
    // you've used the EF designer.
    "res://*/Content.MyModel.csdl",
    "res://*/Content.MyModel.ssdl",
    "res://*/Content.MyModel.msl"
};

Assembly[] assembliesToConsider = new Assembly[]
{
    typeof(MyModel).Assembly
};

System.Data.Metadata.Edm.MetadataWorkspace workspace = new System.Data.Metadata.Edm.MetadataWorkspace(paths, assembliesToConsider);
EntityConnection connection = new EntityConnection(workspace, profiledConnection);

I haven't tested this with the MvcMiniProfiler (first installed it right now), but there might be some issues, if the profiled-connection doesn't exactly behave like the original MySqlConnection, give a try otherwise a will try to create your setup.

Upvotes: 1

Related Questions