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?
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")]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With