Unable to setup MiniProfiler w/ Enity Framework 4.0 (Not code first)

若如初见. 提交于 2020-01-22 13:02:26

问题


I installed MiniProfiler and MiniProfiler.EF in my project via nuget.

Before using MiniProfiler I would open a connection using this in my model repository:

    public class NotificationRepository
    {
       private CBNotificationModel.CB_NotificationEntities db;

       public NotificationRepository()
       {
          db = new CB_NotificationEntities();
       }

       public NotificationContact GetNotificationContacts()
       {
          return db.NotificationContacts.ToList();
       }
    }

To use mini profiler I created:

  public static class ConnectionHelper
    {
        public static CB_NotificationEntities GetEntityConnection()
        {
            var conn = new StackExchange.Profiling.Data.EFProfiledDbConnection(GetConnection(), MiniProfiler.Current);

            return ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(conn); // resides in the MiniProfiler.EF nuget pack
        }

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

The model repository now uses

db = ConnectionHelper.GetEntityConnection();

However this gives the error:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

Am I missing a step? I tried adding MiniProfilerEF.Initialize() and MiniProfilerEF.Initialize_EF42() in Application_start() however that just changes the errors given.

There does not seem to be much information for setting up a entity framework project to use miniprofiler unless it is codefirst.


回答1:


I was able to get this working by changing my ConnectionHelper class to the following:

    public static class ConnectionHelper
    {
            public static CB_NotificationEntities GetEntityConnection()
            {

                var connectionString = ConfigurationManager.ConnectionStrings["CB_NotificationEntities"].ConnectionString;
                var ecsb = new EntityConnectionStringBuilder(connectionString);
                var sqlConn = new SqlConnection(ecsb.ProviderConnectionString);
                var pConn = new StackExchange.Profiling.Data.EFProfiledDbConnection(sqlConn, MiniProfiler.Current);

                var context = ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(pConn);
                return context;

          }
     }


来源:https://stackoverflow.com/questions/11217084/unable-to-setup-miniprofiler-w-enity-framework-4-0-not-code-first

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