Failed to find or load the registered .Net Framework Data Provider for SQL Server Compact Edition

873 Views Asked by At

I get this error:

System.Configuration.ConfigurationErrorsException: 'Failed to find or load the registered .Net Framework Data Provider.'

Also the stack trace of the exception is as below:

at System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key, Func3 handleFailedLookup) at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple2 k)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r) at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key) at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString) at System.Data.Entity.Internal.LazyInternalConnection.Initialize() at System.Data.Entity.Internal.LazyInternalConnection.get_Connection() at System.Data.Entity.Internal.LazyInternalContext.get_Connection() at System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) at System.Data.Entity.Database.Create()

I have added all the below assemblies to my project:

From MySQL connector:

MySql.Data
MySql.Data.Entity 

From SQL Server Compact edition:

System.Data.SqlServerCe
System.Data.SqlServerCe.Entity

And I also added System.Data, and all of them have been set to CopyLocal=True;

I have these lines in my app.config:

<providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
<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.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
         <remove invariant="MySql.Data.MySqlClient" />
         <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

But yet I'm getting the exception.

Also my connection string of the database in which it's creation is creating the exception is as follow:

metadata="res://*/Application Files.Data_Models.ProjectEntitiesModel.csdl|res://*/Application Files.Data_Models.ProjectEntitiesModel.ssdl|res://*/Application Files.Data_Models.ProjectEntitiesModel.msl";provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=C:\Users\jj\Desktop\da\NewDatabase.sdf"

also I have belows in app.config whereas the error is placing when using the above connection string:

<connectionStrings>
    <add name="ComponentDetailsEntities" connectionString="metadata=res://StaticDb/Model1.csdl|res://StaticDb/Model1.ssdl|res://StaticDb/Model1.msl;provider=System.Data.SqlServerCe.4.0;provider connection string='data source=&quot;|DataDirectory|\ComponentDetails.sdf&quot;'" providerName="System.Data.EntityClient" />
    <add name="DefaultProjEntities1" connectionString="metadata=&quot;res://*/Application Files.Data_Models.ProjectEntitiesModel.csdl|res://*/Application Files.Data_Models.ProjectEntitiesModel.ssdl|res://*/Application Files.Data_Models.ProjectEntitiesModel.msl&quot;;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=C:\Users\jj\Desktop\TestHF38.sdf&quot;" providerName="System.Data.EntityClient" />
    <add name="DataModelContainer" connectionString="metadata=&quot;res://*/Application Files.Data_Models_Test.DataModel.csdl|res://*/Application Files.Data_Models_Test.DataModel.ssdl|res://*/Application Files.Data_Models_Test.DataModel.msl&quot;;provider=System.Data.SqlServerCe.4.0;provider connection string='Data Source=&quot;|DataDirectory|\Application Files\Data_Models_Test\DefaultProj2.sdf&quot;'" providerName="System.Data.EntityClient" />
  </connectionStrings>

UPDATE: the problem was that the refrenced System.Data.SqlServerCe.dll was of different version (i think 6) which does not contain sqlProviderFactory. Finally I removed it manually and replaced with 4 in the refrence path of library but I have faced a new error that the version is mismatched whereas I have checked and all of System.Data.SqlServerCe.dll are the same. the error is System.Data.SqlServerCe.SqlCeException: 'Possible file version mismatch detected between ADO.NET Provider and native binaries of SQL Server Compact which could result in an incorrect functionality. This could be due to the presence of multiple instances of SQL Server Compact of different versions or due to wrong binaries with same name as SQL Server Compact binaries. Please install SQL Server Compact binaries of matching version.'

the stack trace of new exception is as follows:

  at System.Data.SqlServerCe.NativeMethods.IsValidBinary(String filename)
   at System.Data.SqlServerCe.NativeMethods.ValidateNativeBinary(String modulePath)
   at System.Data.SqlServerCe.NativeMethods.LoadValidLibrary(String modulePath)
   at System.Data.SqlServerCe.NativeMethods.LoadNativeBinariesFromPrivateFolder(String privateInstall)
   at System.Data.SqlServerCe.NativeMethods.LoadNativeBinaries()
   at System.Data.SqlServerCe.SqlCeConnection..ctor()
   at System.Data.SqlServerCe.SqlCeProviderFactory.CreateConnection()

Please note that I'm using SQL CE 4.0 on win 10. (I dont know if it is related to the problem or not) I have checked all the solutions in the net and stack but the problem is exist yet.Can anyone help me with this please?

0

There are 0 best solutions below