0

I have a project using SqlServerCe.3.5 with EntityFramework 6. I want to change the database to PostgreSQL so I changed the configuration

From:

<entityFramework>
   <providers>
       <provider invariantName="System.Data.SqlServerCe.3.5" type="System.Data.Entity.SqlServerCompact.Legacy.SqlCeProviderServices, EntityFramework.SqlServerCompact.Legacy" />
   </providers>
   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
       <parameters>
           <parameter value="System.Data.SqlServerCe.3.5" />
       </parameters>
   </defaultConnectionFactory>
</entityFramework>
<connectionStrings>
    <add name="Model1Container" connectionString="Data Source=<.. DB.sdf ...>" providerName="System.Data.SqlServerCe.3.5" />
</connectionStrings>
<system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.3.5" />
      <add name="Microsoft SQL Server Compact Data Provider 3.5" 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>

To:

<entityFramework>
   <providers>
       <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
   </providers>
</entityFramework>
<connectionStrings>
    <add name="Model1Container" connectionString="Server=localhost;<...>" providerName="Npgsql" />
</connectionStrings>
<system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
</system.data>

When I run my software with the new config file I got the following error:

DbMigrator migrator = new DbMigrator(new Migrations.Configuration());
var latestMigration = migrator.GetLocalMigrations().Last();
var charPos = latestMigration.IndexOf("_");
var migrationName = latestMigration.Substring(charPos + 1, latestMigration.Length - charPos - 1);
migrator.Update(migrationName);  // <<< it crashes here!!

Exception:

The ADO.NET provider with invariant name 'System.Data.SqlServerCe.3.5' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details. at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader.ThrowOnNonWarningErrors() at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader.LoadItems(IEnumerable'1 xmlReaders, IEnumerable'1 sourceFilePaths) at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader..ctor(IEnumerable'1 xmlReaders, IEnumerable'1 sourceFilePaths, Boolean throwOnError, IDbDependencyResolver resolver) at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Init(IEnumerable'1 xmlReaders, IEnumerable'1 filePaths, Boolean throwOnError, IDbDependencyResolver resolver, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerInvariantName, String& providerManifestToken, Memoizer'2& cachedCTypeFunction) at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection..ctor(IEnumerable'1 xmlReaders) at System.Data.Entity.Utilities.XDocumentExtensions.GetStorageMappingItemCollection(XDocument model, DbProviderInfo& providerInfo) at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion) at System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable'1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable'1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.b__b() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at DAL.Session.Init() in <...>

I got this error message but my PostgreSQL database is created with all the tables defined in my CodeFirst EntityFramework.

In the machine.config there is no SqlServerCe nor Npgsql defined.

Is it possible that somewhere hidden there is a settings I don't see? I made a text search on all config and project files and I don't see any possible reason.

doodoroma
  • 157
  • 2
  • 11
  • Does your code rely on the `System.Data.SqlCe` namespace anywhere? – Joel Coehoorn Mar 19 '18 at 13:52
  • No, I even removed the reference of `SqlServerCe` and I can build. – doodoroma Mar 19 '18 at 14:00
  • @doodoroma check again. SQL Server CE is a *very* specialized database and the 3.5 version is *ancient*. The last version was 4.0 which was discontinued many years ago. It's not used as a default anywhere. If you get such a message it means you still use it. Log the full exception including its call stack and check *where* that error occurs and what call started that call chain. – Panagiotis Kanavos Mar 19 '18 at 14:15
  • @doodoroma did you *debug* your code? Where does that error occur? – Panagiotis Kanavos Mar 19 '18 at 14:16
  • @PanagiotisKanavos I debug and it seems to fail at migration state: ```System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer``` – doodoroma Mar 19 '18 at 14:52
  • Post the code and the exception, not part of the message. Why are you executing migrations if this is a new database though? Do you have any hard-coded references to SQL Server CE in the migration scripts? – Panagiotis Kanavos Mar 19 '18 at 14:59
  • @PanagiotisKanavos I updated the OP. – doodoroma Mar 19 '18 at 15:31
  • @Doodoroma did you check the migrations for any references to the *old* database? Why run migrations at all in a *new* database? – Panagiotis Kanavos Mar 19 '18 at 15:38
  • @PanagiotisKanavos The migration is a must at the beginning of the startup. with the old DB it was working if the tables were empty. How can I check that? I double checked my code and all references, there is no trace of SqlServerCe.3.5 I could see. – doodoroma Mar 19 '18 at 15:48
  • look in the EDMX for SQL CE reference in connection string – magicandre1981 Mar 19 '18 at 15:51
  • @magicandre1981 there is no edmx file. I had one at the beginning of the project though. I moved to Code First because the EDMX didn't support something (I don't remember) which was available in Code First. I extracted the code and removed the edmx file. Is there any chance that my problem is a left over from this? What should I look for? There is no SQL CE in reference, settings, conifg – doodoroma Mar 19 '18 at 16:19
  • delete bin/obj folders to fully rebuild the solution. maybe there is was still a reference to the old SQL CE in the files and they were not updated during build. But **System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader.LoadItems** still looks EDMX related loading. – magicandre1981 Mar 19 '18 at 16:35

1 Answers1

2

SOLUTION! (For those who by any chance get into the same problem )

I ended up removing the migration completely and I rebuilt from scratch. I had used Enable-Migrations which was somehow linked to the current configuration. When I enable migration now, it was running without any problem.

doodoroma
  • 157
  • 2
  • 11