4

EF Code First を使用したクラス ライブラリがあります。EF 4.3 にアップグレードしたばかりで、移行を有効にしたいと考えています。

Enable-Migrations -ProjectName MyProjectNamePM コンソールに入力すると、次のエラーが表示されます

PM> Enable-Migrations -ProjectName MyProjectName
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at System.Data.Entity.Migrations.DbMigrationsConfiguration.GetSqlGenerator(String providerInvariantName)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
The given key was not present in the dictionary.
PM> 

どの辞書が間違っているのかわかりません。

私の接続文字列は次のようになります。

<connectionStrings>
  <add name="MySystem" connectionString="Data Source=MyServer\Instance;Initial Catalog=myDbName;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

何が間違っている可能性がありますか?

注意:
app.config の正確なコピーを使用してコンソール アプリケーションでクラス ライブラリを使用すると、データベースに完全にアクセスできます。

4

3 に答える 3

4

Anders Abel が原因であることが判明しましたが、私たちはもっと簡単な解決策を見つけました。

mvc-mini-profiler のページによると、 Nugetには特別なパッケージがありMiniProfiler.EFSqlConnection. 古い mvc-mini-profiler を削除し、MiniProfiler.EF. その後Enable-Migrations、期待どおりに動作しました。

于 2012-03-09T07:22:44.057 に答える
3

EF Code First には、SQL コード生成用の拡張可能なプロバイダー モデルがあります。のドキュメントにDbMigrationsConfiguration.GetSqlGeneratorは、それが何をするかが書かれています:

特定のデータベース プロバイダーで使用するように設定されている SQL ジェネレーターを取得します。

MvcMiniProfiler は、DB プロバイダーをラップしてプロファイリング サポートを追加します。EF には、MSSQL DB ではなく MvcMiniProfiler DB を使用しているように見えます。残念ながら、EF Code はまず MvcMiniProfiler DB の処理方法を知りません。

可能な修正は、Sql Server ジェネレーターをラップする MvcMiniProfiler 名を持つ SqlGenerator を追加することです。

編集

既存の sql サーバー ジェネレーターを mvc ミニ プロファイラー名に再登録するだけでよいようです (その名前がわかれば)。

http://romiller.com/2012/01/16/customizing-code-first-migrations-provider/には、プロバイダーの登録方法を示すコード スニペットがあります。

public Configuration()
{
    AutomaticMigrationsEnabled = false;

    SetSqlGenerator("System.Data.SqlClient", 
        new CustomMigrationsProviders.CustomSqlServerMigrationSqlGenerator());
}
于 2012-03-08T17:41:46.673 に答える