Reputation: 10839
Unable to cast object of type 'MvcMiniProfiler.Data.EFProfiledDbConnection' to type 'System.Data.SqlClient.SqlConnection'.
I am trying to upgrade to MvcMiniProfiler 1.9.0 and I keep getting this when I call MiniProfilerEF.Initialize(). I have removed the system.data config section. I don't know what I am doing wrong. I have followed the steps on the site, but maybe I missed something?
I am using EF code first 4.1 and I am passing in the name of my connectionstring into a constructor to create my datacontext.
Web Activator
using Project.Web.App_Start;
using WebActivator;
[assembly: PreApplicationStartMethod(typeof(MiniProfiler), "Start")]
namespace Project.Web.App_Start {
public class MiniProfiler {
public static void Start()
{
if (Eco.Environment.IsDevelopment) {
MiniProfilerEF.Initialize();
}
}
}
}
StructureMap Registry:
using Project.Domain.Repositories;
using StructureMap.Configuration.DSL;
namespace Project.Web.DependencyResolution.Registries {
public class RepositoriesRegistry : Registry {
public RepositoriesRegistry() {
For<IProjectDataContext>().HybridHttpOrThreadLocalScoped().Use(() => new ProjectDataContext(Eco.Database.Name));
}
}
}
DataContext Constructor:
public ProjectDataContext(string nameOrConnectionString)
: base(nameOrConnectionString) {
Active = new Active(this);
}
I have removed system.data dataproviders fron my config since the documentation says I only need to call MiniProfilerEF.Initialize().
**Update
Previously in 1.7 MvcMiniProfiler I had to set the Database.DefaultConnectionFactory property, but I've removed that. The Database.DefaultConnectionFactory always comes back as SqlConnectionFactory, shouldn't it be ProfiledConnectionFactory or something like that?
Upvotes: 8
Views: 2809
Reputation: 976
I made the mistake of adding MiniProfiler.EF rather than MiniProfiler.EF6. Removing MiniProfiler.EF and replacing it with the EF6 version fixed my issue.
Upvotes: 2
Reputation: 721
see https://stackoverflow.com/a/10814033/311289
This is caused by doing DB operations before initializing miniprofiler, put a breakpoint in the contstructor for your db context, and another on the MiniProfilerEF.Initialize();
line, and revise until the initialization is first.
Upvotes: 1
Reputation: 126
I had this same problem and the way I found to fix it was to move to Glimpse: http://getglimpse.com/. In my opinion, it is a lot better than miniprofiler, easy to use, complete, etc.
Upvotes: 0
Reputation: 108346
I was seeing this same error. It drove me nuts but I finally figured it out. My issue had nothing to do with web.config
, assemblies, Initialize_42
or Initialize(false)
hacks or anything.
Here's where I went wrong...
I had enabled automatic application of migrations like this:
App_Start:
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<DataContext, Migrations.Configuration>()
);
Migrations/Configuration.cs:
internal sealed class Configuration
: DbMigrationsConfiguration<Path.To.DataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
}
And that was being triggered via WebActivator like this:
[assembly: WebActivator.PreApplicationStartMethod(
typeof(service_tracker_mvc.App_Start.DatabaseInitializer), "Start")]
I accidentally found that disabling this process resulted in the profiler working. The issue, as it happens, is that this init process was happening too soon. It normally happens during Application_Start
(if you're not using this fancy WebActivator stuff) so I changed it to PostStart
. Now it works:
▼▼▼▼
[assembly: WebActivator.PostApplicationStartMethod(
typeof(service_tracker_mvc.App_Start.DatabaseInitializer), "Start")]
Upvotes: 2
Reputation: 487
The problem I see here, is that ProjectDataContext encapsulates some data context into property "Active", which could not be found by MvcProfiler proxy.
More than that, EFProfiledDbConnection is actually DbConnection child, but not SqlConnection's child. It is made in terms of abstraction to use different Db providers, like MySql, Postgres, etc. Please, try to review all variables in code, they should be DbConnection, but not SqlConnection (this is a provider of MsSql).
Upvotes: 0