Unable to cast object of type 'MvcMiniProfiler.Data.EFProfiledDbConnection' to type 'System.Data.SqlClient.SqlConnection'
Asked Answered
W

5

8

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?

Wares answered 29/12, 2011 at 15:15 Comment(6)
Can you provide code + config file? Typically, you define the connection string in the config file of the same name as your DBContext class, and you never need to use a non default constructor.Asis
Iirc there is an EF sample setup on nuget. But: can I ask: do you always give it a profiled connection? Or do you sometimes (perhaps depending on the user) use a profiles connection, and sometimes use a naked SQL connection?Airliah
@MarcGravell I did this with version 1.7. I would switch from a profiled connection (Debug) to sql connection in (Release). It worked fine.Wares
@Abuhakmeh that's not the same as I was describing though, as that is always the same in a single AppDomain.Airliah
@MarcGravell Then the answer to your question is that I always use the same type of connection determined at the start of the application. It only changes when I restart the app. When developing it's always profiled.Wares
if you want EF 4.1 to work you are going to need use latest .. not nugetLouettalough
C
2

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")]
Crowned answered 5/4, 2012 at 19:0 Comment(0)
I
2

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.

Illegality answered 12/1, 2017 at 6:59 Comment(1)
Thank you Sam, this saved me.Entozoon
T
1

see https://mcmap.net/q/1471368/-mvcminiprofiler-unable-to-cast-object-of-type-efprofileddbconnection

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.

Tetrameter answered 30/4, 2014 at 13:29 Comment(0)
C
0

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).

Carlsbad answered 1/3, 2012 at 16:22 Comment(0)
H
0

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.

Hesky answered 9/1, 2014 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.