加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 无法使用Enity Framework 4.0设置MiniProfiler

发布时间:2020-12-16 06:31:23 所属栏目:asp.Net 来源:网络整理
导读:我通过nuget在我的项目中安装了MiniProfiler和MiniProfiler.EF. 在使用MiniProfiler之前,我会在我的模型库中使用它来打开一个连接: public class NotificationRepository { private CBNotificationModel.CB_NotificationEntities db; public NotificationRe
我通过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似乎没有太多信息,除非它是代码优先的.

解决方法

我能够通过将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;

          }
     }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读