Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# program is looking for sql server compact 3.5, but I'm using 4.0

Okay, this one is really puzzling me, and I apologize if the title of the question doesn't accurately describe what my problem is.

I have a program made in C#, using Entity Framework and SQL Server CE. I developed it on Windows 7, and it works fine. I also have a Windows 8.1 machine that I've tested it on, and it works fine on that too.

However, on a few Windows 8.1 computers, the program crashes as soon as it tries to access the database. I get the following error:

Unhandled Exception: System.InvalidOperationException: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlServerCe.3.5'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

Now, this really confuses me, because I have the latest NuGet package for SQL Server CE installed on my project (4.0). I used the SQL Server CE toolbox to explicitly create a SQL Server CE 4.0 database, I distributed the 4.0 dll's with the application, and I explicitly have version 4.0 listed in my app.config file (invariantName="System.Data.SqlServerCe.4.0").

So, why does the error say System.Data.SqlServerCe.3.5? Why does this error only occur on a couple of Windows 8.1 machines?

(I also searched through my entire solution, and the text "3.5" doesn't even appear anywhere.)

I found similar errors, and I tried (from this question) to include this:

private volatile Type _dependency;

public MyClass()
{
    _dependency = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
}

Has no effect, I still get the same exact answer.

Anyways, if anybody has any ideas, I would greatly appreciate any kind of input.

like image 222
Steven Jeffries Avatar asked Aug 01 '14 08:08

Steven Jeffries


2 Answers

I think problem is both SQL Server CE components installed in your system.

My solution :

<DbProviderFactories>
    <remove invariant="System.Data.SqlServerCe.3.5" />
    <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.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
like image 64
Muhammet Emin ERDOĞAN Avatar answered Sep 18 '22 05:09

Muhammet Emin ERDOĞAN


I had the same issue and solved it based on this link.

Find this section in your App.config:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

And change oldVersion="0.0.0.0-4.0.0.1" to oldVersion="4.0.0.0-4.0.0.1"

like image 44
Kohanz Avatar answered Sep 19 '22 05:09

Kohanz