【问题标题】:Unable to setup MiniProfiler w/ Enity Framework 4.0 (Not code first)无法使用/Entity Framework 4.0 设置 MiniProfiler(不是代码优先)
【发布时间】:2012-06-26 22:55:17
【问题描述】:

我通过 nuget 在我的项目中安装了 MiniProfiler 和 MiniProfiler.EF。

在使用 MiniProfiler 之前,我会在我的模型存储库中使用它打开一个连接:

    public class NotificationRepository
    {
       private CBNotificationModel.CB_NotificationEntities db;

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

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

要使用我创建的迷你分析器:

  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);
        }
    }

模型库现在使用

db = ConnectionHelper.GetEntityConnection();

但是这会产生错误:

在 mscorlib.dll 中发生“System.StackOverflowException”类型的未处理异常

我错过了一步吗?我尝试在 Application_start() 中添加 MiniProfilerEF.Initialize() 和 MiniProfilerEF.Initialize_EF42() 但这只会改变给出的错误。

似乎没有太多关于设置实体框架项目以使用 miniprofiler 的信息,除非它是 codefirst。

【问题讨论】:

    标签: asp.net-mvc-3 entity-framework mvc-mini-profiler


    【解决方案1】:

    我可以通过将我的 ConnectionHelper 类更改为以下内容来完成这项工作:

        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;
    
              }
         }
    

    【讨论】:

    • 我希望我能给你这么多分,我找遍了这个
    • 哦,是的,谢谢。这是我第二次尝试让 MiniProfiler 工作,每次我得到一个 StackOverflowException。此解决方案完美运行。
    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 2022-11-17
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多