应用程序配置文件中的连接字符串‘ MyConnection’不包含所需的 ProviderName 属性。”

我用 Entity Framework Code First,

我的连接字符串在配置文件中:

<connectionStrings>
<clear/>
<add name="ApplicationServices" connectionString="Data Source=PC-X;Initial Catalog=MYdb;Integrated Security=True"/>
</connectionStrings>

当我尝试访问数据(应该创建数据库的内容)时,出现以下错误:

应用程序的 配置文件不包含所需的 ProviderName 属性”

我错过了什么?

67574 次浏览

You're missing the following piece of code after the connectionString attribute (assuming that you're using SQL):

providerName="System.Data.SqlClient"

Sometime in the future. the complete code

<add name="YouContext" connectionString="Integrated Security=True;Persist Security Info=False;Initial Catalog=YourDatabaseName;Data Source=YourPCName;" providerName="System.Data.SqlClient"/>

Go down in your web.config until you reach the providers tag. For instance, here's my providers statement:

<providers><provider invariantName="System.Data.SqlClient" ... /></providers>

you should add this System.Data.SqlClient as a provider name in your connection string so your connection string should look like this:

  <connectionStrings>
<add name="ApplicationServices" providerName="System.Data.SqlClient" connectionString="Data Source=PC-X;Initial Catalog=MYdb;Integrated Security=True"/>
</connectionStrings>


In my case the problem was with an incorrect StartUp project target. In the PM console the target migration assembly project was correct.

I have a multiproject solution and the target was on some web-service project.

So I changed the StartUp to the main WebSite project and the migration have complited without errors.