在不指定 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.