MvcMiniProfiler Unable to cast object of type EFProfiledDbConnection

心已入冬 提交于 2019-11-30 20:30:19
user1425714

Maybe this will help. Move the MiniProfilerEF.Initialize(); to the top of the method Application_Start(). Note that in EF 4.1 and higher the method called should be MiniProfilerEF.Initialize_EF42(); instead.

protected void Application_Start() {
    Logger.Info("Application start");
    MiniProfilerEF.Initialize_EF42();
    // ...
}
Shelakel

Check out http://code.google.com/p/mvc-mini-profiler/

Here's an example of how to use mvc-mini-profiler with EF Database First:

public static class Entities
{
    public static MyEntities Create()
    {
         var builder = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString);
         var sqlConnection = new SqlConnection(builder.ProviderConnectionString);
         var profiledConnection = new EFProfiledDbConnection(sqlConnection, MiniProfiler.Current);

         return profiledConnection.CreateObjectContext<MyEntities>();
    }
}

You can then register your entities with an IOC container using the method or alternatively use something like

using(var entities = Entities.Create())
{
    //Do stuff here
    entities.SaveChanges();
}

Edit: Forgot to add

MiniProfilerEF.Initialize();

That is only used for EF Code First.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!