15

We've been using Entity framework in a production environment for many months now, and just yesterday started to get errors on some machines when querying the database using our subclass of DbContext:

"The connection string 'MyConnectionString' in the application's configuration file does not contain the required providerName attribute"

Our problem is easily solved: I adding the "providerName="System.Data.SqlClient" to the connection string in the config files on all deployed servers and workstations.

However, the mystery remains: according to the documentation:

The providerName attribute is optional, and the default is "System.Data.SqlClient".

Even more mysterious is why this started happening suddenly, and apparently only on some machines. I am not aware of any recent changes in EF or .NET versions, any SQL Server version or provider changes, or anything. But I realize there has to be something I've overlooked.

.NET 4.5 EF 5.0

Anyone have any hints or insights?

Sandy
  • 11,332
  • 27
  • 76
  • 122
esmoore68
  • 1,276
  • 1
  • 8
  • 16
  • My development machine started to do this to me once I installed the MySQL ADO.NET connector for a different project... but MySQL appears to add itself to the machine.config file. I just assumed rather than System.Data.SqlClient being the default - it is more accurate to say it's optional if there is only one configured entity framework provider. – dannykay1710 Jan 13 '14 at 13:48

2 Answers2

16

Certain driver combinations will cause the machine to be in a state where it is ambiguous which driver it should use, so it requires an explicit provider name.

It was probably some other separate application or driver install, or automatic Windows Update that ran.

Being explicit with the provider name doesn't hurt anything though. You should be fine adding it; it's only a few extra characters in your connection string. It won't ever need to change in the future or anything.

Your updated declaration should read:

<connectionStrings>
  <add
    name="MyConnectionStringName"
    connectionString="Connection string goes here"
    providerName="System.Data.SqlClient" />
</connectionStrings>
Jon Adams
  • 24,464
  • 18
  • 82
  • 120
  • 1
    Hi, sorry for the stupid question. But what does exactly providerName mean here? Is it the namespace? Does it refer to a specific driver/DLL? – Steve3p0 Feb 27 '16 at 04:16
  • 1
    Provider is generally about the same as a driver in this case, yes. – Jon Adams Feb 27 '16 at 06:32
4

You can use too:

providerName="System.Data.EntityClient"

then:

<add name="name_here" connectionString="Data Source="pathofdatabase" providerName="System.Data.EntityClient" />
Diego Venâncio
  • 5,698
  • 2
  • 49
  • 68