Troubleshooting: Privately deploying the SQL Server Compact 3.5 SP2 ADO.NET Entity Framework provider (System.Data.SqlServerCe.Entity.dll) in the application folder does not work

For complete instructions about deploying SQL Server Compact ADO.NET Entity Framework provider (System.Data.SqlServerCe.Entity.dll) privately in the application’s folder refer to the blog here. Note that from SQL Server Compact 3.5 SP2 release onwards if you are privately deploying SQL Server Compact 3.5 SP2 DLLs in your application folder you need to copy the managed DLLs (System.Data.SqlServerCe.dll and System.Data.SqlServerCe.Entity.dll) from the location %Program Files%\Microsoft SQL Server Compact Edition\v3.5\Private. Refer to the SQL Server Compact 3.5 SP2 Books Online for more information.

Symptoms

When an application uses System.Data.SqlServerCe.Entity.dll from SQL Server Compact 3.5SP2 in xcopy (i.e. private) deployment mode by copying the Compact’s ADO.NET provider (System.Data.SqlServerCe.dll) and the Compact’s ADO.NET EF provider (System.Data.SqlServerCe.Entity.dll) DLLs from the %Program Files%\Microsoft SQL Server Compact Edition\v3.5\Private, following exception is thrown:
"System.IO.FileLoadException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"

Cause

System.Data.SqlServerCe.Entity.dll and System.Data.SqlServerCe.dll from the Private folder is of version 3.5.1.50. But unfortunately, the System.Data.SqlServerCe.Entity.dll from the Private folder (i.e. with version 3.5.1.50) is referring to System.Data.SqlServerCe.dll from the Desktop folder (i.e. with version 3.5.1.0). Due to this, an application that has only the dlls from Private folder fails as it doesn't find the 3.5.1.0 version of System.Data.SqlServerCe.dll.

Resolution

Add assembly binding redirection entry in app.config file.
Append following lines under the configuration section of the app.config:

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

- Vipul Hattiwale