使用 MvcMiniProfiler 监控EF 4.1 with MySQL Provider
2011-08-01 19:46
441 查看
本文有2个不怎么相关的话题,一是用MVCMiniProfiler 监控EF4.1,另一个则是为MvcMiniProfiler提供MySQL Provider的支持。
需要在Web.config中添加system.data的节点
[/code]
另外在Application_Start设置Database.DefaultConnectionFactory即可
[/code]
当然,使用MySQL的话需要使用MySQL的ConnectionFactory,但是MySQL没有为我们提供,所以我们就自己写一下
[/code]
然后使用MySqlConnectionFactory来注册
[/code]
但是有很多时候使用EF4.1这样做并不会生效我们可能还要为DbContext包装一个基类来实现如下功能
[/code]
需要在Web.config中添加system.data的节点
[code] 1: <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
2: <add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider"
3: description="MvcMiniProfiler.Data.ProfiledDbProvider"
4: type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.7.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
[/code]
另外在Application_Start设置Database.DefaultConnectionFactory即可
[code] 1: var factory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
2: var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
3: Database.DefaultConnectionFactory = profiled;
[/code]
当然,使用MySQL的话需要使用MySQL的ConnectionFactory,但是MySQL没有为我们提供,所以我们就自己写一下
[code] 1: class MySqlConnectionFactory : IDbConnectionFactory
2: {
3: public DbConnection CreateConnection(string nameOrConnectionString)
4: {
5: using (MiniProfiler.Current.Step("创建Connection:" + nameOrConnectionString))
6: {
7: var connStr = ConfigurationManager.ConnectionStrings[nameOrConnectionString];
8: return new MySqlConnection(connStr.ConnectionString);
9: }
10: }
11: }
[/code]
然后使用MySqlConnectionFactory来注册
[code] 1: var factory = new MySqlConnectionFactory();
2: var profiled = new ProfiledDbConnectionFactory(factory);
3: Database.DefaultConnectionFactory = profiled;
[/code]
但是有很多时候使用EF4.1这样做并不会生效我们可能还要为DbContext包装一个基类来实现如下功能
[code] 1: abstract public class BaseDbContext :DbContext
2: {
3: protected BaseDbContext(string nameOrConnectionString)
4: : base(GetConnection(nameOrConnectionString),true)
5: {
6:
7: }
8:
9: private static DbConnection GetConnection(string nameOrConnectionString)
10: {
11: if( Database.DefaultConnectionFactory is SqlConnectionFactory)
12: {//这个判断是为了解决未使用MySqlConnectionFactory时自动使用SQLServer的问题
13: var connStr = ConfigurationManager.ConnectionStrings[nameOrConnectionString];
14: return new MySqlConnection(connStr.ConnectionString);
15: }
16: return Database.DefaultConnectionFactory.CreateConnection(nameOrConnectionString);
17: }
18: }
[/code]
相关文章推荐
- ASP.NET MVC5 使用MiniProfiler 监控MVC性能
- 【MVC】ASP.NET MVC5 使用MiniProfiler 监控MVC性能
- 实例分析ASP.NET在MVC5中使用MiniProfiler监控MVC性能的方法
- 使用MiniProfiler跟踪MVC + EF + Bootstrap 2 权限管理系统的性能消耗
- asp.net mvc ef 性能监控调试工具 MiniProfiler
- MVC教程--MiniProfiler.EF监控调试MVC和EF的性能
- 使用MiniProfiler给Asp.net MVC和Entity Framework号脉(附源码)
- 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)
- MVC中使用EF+MySQL
- 使用MiniProfiler给Asp.net MVC和Entity Framework号脉(附源码)
- 1.采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)
- 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)(转)
- 使用MiniProfiler给Asp.net MVC和Entity Framework号脉(附源码)
- 使用MiniProfiler调试Asp.net Mvc性能
- Dapper with MVC MiniProfiler
- ABP使用Miniprofiler监测EF
- 使用MiniProfiler调试ASP.NET MVC网站性能
- 使用MiniProfiler调试ASP.NET MVC网站性能
- 使用性能优化工具 MVC Mini Profiler (MVC3+EF4.1)
- ASP.NET MVC5使用MiniProfiler监控MVC性能