Beware machine.config files messing up Code First provider factories

It took us a long time and a bit of luck to get to the bottom of this problem. It turns out that at two completely different client sites they had computers whose machine.config file had been affected in a way that was causing an error in Code First.

The error that was being thrown at runtime was:

“Unable to determine the provider name for connection of type ‘System.Data.SqlClient.SqlConnection’”

Yet the database was reachable from ADO.NET code in the app that used the same connection string.

This led us down a lot of exploration of the provider settings in the web.config file.

Finally a clever developer at the client site wrote a little debugging tool that helped us see the *true* error which helped him get to the bottom of the problem.

The real error was:

"’DbProviderFactories’ section can only appear once per config file error’"

And it turned out that the machine.config had extra DbProviderFactories sections in it’s element.

We are still trying to figure out what may have created this problem in machine.config but in the meantime, I’m trying to create some searchable text to help the next person who hits this problem.

I’ve ensured that the EF team is already aware of the problem.

Here is the MSDN forum thread related to this problem along with the solution:

Unable to determine the provider name for connection of type ‘System.Data.SqlClient.SqlConnection’.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

3 thoughts on “Beware machine.config files messing up Code First provider factories

  1. This is an old post, but Google still ranks it highly when searching for this error, so here’s something additional I found that might help others:

    this seems to be caused by third-party drivers having buggy installers. They add a section for themselves, but they don’t properly merge with an existing section or delete it if it is empty. I’ve run into it with the IBM System i (a.k.a. AS/400) client and I think I had problems previously with the Data Direct driver install.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.