Constructor on type 'System.Data.Entity.Infrastructure.SqlConnectionFactory' not found

13,031

Solution 1

I fixed the problem. I changed this:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

To:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>

And now it works. A bit stupid though, I'd like to supply the connection string as a parameter.

I found this out by creating a new MVC 4 project with entity framework in VS 2012, and comparing the Web.Config files. I noticed another difference.

My Entity Framework assembly has version 4.4.0.0 (although I installed the 5.0 RC from NuGet). The new project that I created has an assembly with version 5.0.0.0. Can anyone explain that?

Solution 2

A couple of things you could check:

  • It is trying to connect to SQL Express. Is SQL Express installed? Did something happen to the installation when you upgrated windows.
  • What is the security context of the program trying to connect to SQL Express? Could it be a rights issue?

Edit A couple more ideas:

  • Related to the .net framework version selected for the project
  • Related to how EF is referenced, are all referenced? Copy local = true?
Share:
13,031
Leon Cullens
Author by

Leon Cullens

Available for freelance jobs. Contact me at [email protected].

Updated on June 13, 2022

Comments

  • Leon Cullens
    Leon Cullens almost 2 years

    I "upgraded" my PC from Windows 7 to Windows 8 yesterday, so now I'm using Visual Studio 2012, and opening my Visual Studio 2010 project.

    This project always worked fine, but it doesn't work in Visual Studio 2012. I had some weird errors, but I fixed those. Now I'm left with one error that I just can't fix and can't find anything about on Google:

    I get this exception:

    Failed to set Database.DefaultConnectionFactory to an instance of the 'System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework' type as specified in the application configuration. See inner exception for details.

    With the following innerexception:

    Constructor on type 'System.Data.Entity.Infrastructure.SqlConnectionFactory' not found.

    My Web.Config looks pretty standard:

      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-WebUI-201253192737;Integrated Security=True" providerName="System.Data.SqlClient" />
      </connectionStrings>
    
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
          </parameters>
        </defaultConnectionFactory>
      </entityFramework>
    

    The SqlConnectionFactory class exists and it has 2 constructors (one parameterless, and one that takes a connection string). I tested that by creating a SqlConnectionFactory in my C# code.

    I already deleted Entity Framework and reinstalled it, but that didn't solve the problem.

    I'm using Entity Framework 5.0 RC (which worked fine in VS2010 as stated before) with SQL Server 2012 + Tools and IIS express.

  • Leon Cullens
    Leon Cullens almost 12 years
    1) yes, I have SQL Server 2012 + Tooling installed, and I can connect to the SQL Server via SSMS.
  • Leon Cullens
    Leon Cullens almost 12 years
    2) I gave every available role to every available user, didn't work. That would result in a different error anyway (had that before on Windows 7). This error clearly has sometime to do with the EF library (it can't find the constructor?).
  • Leon Cullens
    Leon Cullens almost 12 years
    The .NET version for all the projects is 4.0 and copy local is true.
  • matto0
    matto0 over 11 years
    I highly recommend NOT using the code above for the purposes of this question. -pre installs the latest pre-release version of the package, as of this post it installs EF6 alpha.