Jakob Lithner
Jakob Lithner

Reputation: 4426

SqlCe 4.0 + EF 5.0 stopped working in VS2012

I have created an application almost ready to distribute to clients. It took my several months. Everything worked fine but then I installed VS2012 and all my tests stop working. I tried everything but finally gave up and reinstalled my whole computer.

Same setup as before but everything clean: Windows 7 64bit, VS2012, SQL2012, etc. Now all tests worked fine so I was happy. But now I ran into a new trouble. When targeting SqlCe 4.0 it does not work. My application will sync with a central SQL Server through WinApi. That part was fine, but the local database with SqlCe 4.0 just does NOT work. What is the problem? It is a new clean computer!

I created a fresh new solution with basic setup: Nuget installed SqlCe Compact, Entity Framework 5, code first, one class, one context. It does not work! It is stuck on first context call to database and database file is not generated. No exception, just stuck!

What is going on? Is SqlCe not reliable? Should I switch to SQLite? Problem is I have lots of code and I am very used to EF.

Upvotes: 0

Views: 836

Answers (2)

ErikEJ
ErikEJ

Reputation: 41819

Installing the NuGet package does not register anything in machine.config, you must remove/add the provider in your own config file, or install the 4.0 MSI file on the users computer. The EF samples always assume that the MSI is installed.

Sample config entry:

<system.data>    
  <DbProviderFactories>      
    <remove invariant="System.Data.SqlServerCe.4.0"/>      
    <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>    
  </DbProviderFactories>  
</system.data>

I have a blog post on private deplyment here: http://erikej.blogspot.dk/2011/02/using-sql-server-compact-40-with.html

Upvotes: 2

Jakob Lithner
Jakob Lithner

Reputation: 4426

I searched without finding an answer exactly matching my case. But I got an idea on a blog mentioning a bad formed tag in the machine.config file. When looking in it I found this very crucial section:

  <system.data>
    <DbProviderFactories>
      <add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
      <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
    </DbProviderFactories>
  </system.data>

In my machine.config file the section DbProviderFactories was empty. I tried to add it but was not able, because it was locked by some application. On the other hand my customers may run into the same problem and it is difficult for me to have them edit machine.config. So I did it the easy way: added the section to my application config file! Voila! Everything is back to normal. My application runs beautifully again.

But why did it work in my previous Windows installation? Why is this section not always added to machine.config? Why is DbProviderFactories never mentioned as a prerequisite in demo samples on SqlCe 4.0 in combination with EntityFramework 5.0? Is this maybe just a problem in .Net Framework 4.5?

Upvotes: 0

Related Questions