ADO.NET 正在寻找我不想使用的 DbProvider

ADO.NET is looking for a DbProvider which I don't want to use

我有一个使用 SqlServerCe.3.5 的项目 EntityFramework 6. 我想将数据库更改为 PostgreSQL,所以我更改了配置

发件人:

<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>

收件人:

<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>

当我 运行 我的软件使用新的配置文件时,出现以下错误:

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!!

异常:

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 <...>

我收到此错误消息,但我的 PostgreSQL 数据库是使用我的 CodeFirst EntityFramework 中定义的所有表创建的。

machine.config 中没有定义 SqlServerCe 和 Npgsql。

有没有可能隐藏的某个地方有我看不到的设置?我对所有配置和项目文件进行了文本搜索,但没有发现任何可能的原因。

解决方案! (对于那些偶然遇到同样问题的人)

我最终完全删除了迁移并从头开始重建。我使用了 Enable-Migrations ,它以某种方式链接到当前配置。当我现在启用迁移时,它 运行 没有任何问题。