0
votes

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.

1
Does your code rely on the System.Data.SqlCe namespace anywhere?Joel Coehoorn
No, I even removed the reference of SqlServerCe and I can build.doodoroma
@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
@doodoroma did you debug your code? Where does that error occur?Panagiotis Kanavos
@PanagiotisKanavos I debug and it seems to fail at migration state: System.Data.Entity.Migrations.Infrastructure.EdmModelDifferdoodoroma

1 Answers

2
votes

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.