22

最近、Entity Framework のデータ移行に切り替え、MVC アプリのビルド自動化スクリプトに取り組んでいます。4.3 の migrate.exe ツールを使用してビルド サーバーからの移行を正常に実行できます (それを指す Web.config がある場合)。コマンドは次のようになります。

ProjectName\packages\EntityFramework.4.3.1\tools\migrate.exe MyAssembly
    /startupdirectory:ProjectName\bin\Debug 
    /startupconfigurationfile:ProjectName\Web.config 
    /verbose

ただし、さまざまな理由から、Web.config の使用を避け、移行時に正しい接続文字列を渡したいと考えています。

ProjectName\packages\EntityFramework.4.3.1\tools\migrate.exe MyAssembly
    /startupdirectory:ProjectName\bin\Debug 
    /connectionString:"Data Source=awesomeserver;Initial Catalog=awesomedatabase;User Id=funkyuser;Password=crazypassword" 
    /verbose

これは動作しません。さらに悪いことに、Migrate.exe が NullReferenceException でクラッシュします。接続文字列は、Web.config で使用するものと同じです。

誰もこれに遭遇しましたか?接続文字列の形式が間違っていますか? バグ?

4

2 に答える 2

24

わかりました、私たちはそれを理解しました。Web.config なしで実行する場合は、connectionProviderName パラメーターも渡す必要があります。

ProjectName\packages\EntityFramework.4.3.1\tools\migrate.exe MyAssembly
    /startupdirectory:ProjectName\bin\Debug 
    /connectionProviderName:"System.Data.SqlClient"
    /connectionString:"Data Source=awesomeserver;Initial Catalog=awesomedatabase;User Id=funkyuser;Password=crazypassword" 
    /verbose

これが機能することを確認しました。

于 2012-03-14T18:03:14.567 に答える
12

web/app.config ファイルを指定せずに実際に機能するソリューションをまだ見つけていません。下記参照。

ただし、web/app.config を提供し、接続文字列をコマンドライン パラメーターとしてオーバーライドすることを受け入れることができる場合、以下は Entity Framework 5.0 nuget および .NET 4.5 で機能します。.NET 4.0 でも文書化された回避策で動作するはずです。

フォルダ構造の例:

trunk\MySolution.sln
trunk\run_migration.bat

trunk\MyMvc4App\MyMvc4App.csproj 
trunk\MyMvc4App\web.config

trunk\MyMvc4App\bin\MyMvc4App.dll
trunk\MyMvc4App\bin\EntityFramework.dll

trunk\packages\EntityFramework.5.0.0\tools\migrate.exe

run_migration.bat:

SET AssemblyName=MyMvc4App
SET StartUpDirectory=MyMvc4App\bin\
SET ConnectionString=Server=tcp:XXXX.database.windows.net,1433;Database=XXXX;User ID=XXXX;Password=XXXX;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True
SET ConnectionStringProvider=System.Data.SqlClient
SET ConfigFilePath=%CD%\MyMvc4App\web.config
SET MigrateExe=packages\EntityFramework.5.0.0\tools\migrate.exe

%MigrateExe% %AssemblyName%.dll /startUpDirectory:%StartUpDirectory% /startUpConfigurationFile:"%ConfigFilePath%" /connectionProviderName:"%ConnectionStringProvider%" /connectionString:"%ConnectionString%" /verbose
pause

ソリューションの終了。


構成ファイルの省略:

構成ファイルを省略しようとすると、何を試しても常に次の例外が発生しました。私は EF 4.3 を試していないので、4.3 と 5.0 の間で動作が変わったのではないかと思います。

System.Data.Entity.Migrations.Design.ToolingException: Exception has been thrown by the target of an invocation.
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.Console.Program.Run()
   at System.Data.Entity.Migrations.Console.Program.Main(String[] args)
ERROR: Exception has been thrown by the target of an invocation.
于 2013-01-03T12:17:31.520 に答える