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

纵饮孤独 提交于 2020-03-01 05:39:25

问题


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.


回答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?




回答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?



回答3:


I believe a good way to upgrade from EF v4.3 to Ef v5.0 for existing projects is to use the Nuget command

update-package EntityFramework -pre

This updates the configsection in the config file to EF 5



来源:https://stackoverflow.com/questions/10965210/constructor-on-type-system-data-entity-infrastructure-sqlconnectionfactory-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!