我正在尝试使用带有EFCodeFirst的mvc-mini-profiler我正在创建一个DbProfiledConnection并将其传递给DbContext,如下所示.应用程序继续按照预期工作,sql不会暴露给Profiler.
- public class WebContext : DbContext
- {
- static DbConnection _connection = new sqlConnection(ConfigurationManager.ConnectionStrings["WebContext"].ConnectionString);
- static DbConnection _profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(_connection);
- public WebContext()
- : base(_profiledConnection,true)
- {
- }
哎哟我的坏
我已修改它,以便当我的WebContext构造在我的UnitOfWork中,我传入一个ProfiledDbConnection
- public UnitOfWork()
- {
- var profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(connection);
- this.context = new MyContext(profiledConnection);
- }
我已经检查过,并且在Application_BeginRequest中设置了MiniProfier Current,并且当我尝试查询数据库时在ProfiledDbProviderServices类中抛出一个错误时返回一个ProfiledDbConnection.
- protected override string GetDbProviderManifestToken(DbConnection connection)
- {
- return tail.GetProviderManifestToken(connection);
- }
解决方法
我怀疑这与静态字段初始化器有关.网络应用程序上的连接不应该是静态的(最多是请求特定的).
关键是:ProfiledDbConnection是什么实际出来的?只有当您正在对当前请求进行分析时,Get方法才会返回ProfilingDbConnection,并且根据该请求针对MiniProfiler实例对连接进行概要分析.
如果使用静态字段,则有两种情况:
>静态字段在没有请求上下文(或非开发者请求上下文)的情况下被初始化):不会发生分析,因为MiniProfiler.Current为null>静态字段被初始化,但是一切都按照第一个请求进行记录,这个请求很快死亡