.net 在没有Web/App.config的情况下运行Entity Framework migrate.exe工具的正确格式是什么?

.net 在没有Web/App.config的情况下运行Entity Framework migrate.exe工具的正确格式是什么?,.net,entity-framework-4,connection-string,entity-framework-migrations,.net,Entity Framework 4,Connection String,Entity Framework Migrations,我们最近转向了实体框架数据迁移,我正在为我们的MVC应用程序编写一些构建自动化脚本。如果我有一个Web.config指向,我可以使用4.3中的migrate.exe工具从构建服务器成功运行迁移。该命令类似于: ProjectName\packages\EntityFramework.4.3.1\tools\migrate.exe MyAssembly /startupdirectory:ProjectName\bin\Debug /startupconfigurationfil

我们最近转向了实体框架数据迁移,我正在为我们的MVC应用程序编写一些构建自动化脚本。如果我有一个Web.config指向,我可以使用4.3中的migrate.exe工具从构建服务器成功运行迁移。该命令类似于:

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中使用的字符串相同


以前有人遇到过这种情况吗?我的连接字符串格式是否错误?Bug?

好的,我们找到了答案。在没有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

我已经证实了这一点。

我还没有找到一个在不指定web/app.config文件的情况下实际有效的解决方案。见下文

但是,如果您可以接受提供web/app.config并将连接字符串作为命令行参数重写,那么以下内容适用于Entity Framework 5.0 nuget和.NET 4.5。也适用于.NET4.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
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
运行\u迁移。bat:

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
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
解决方案结束


忽略配置文件:

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
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
如果试图省略配置文件,无论我做了什么尝试,都会出现以下异常。我没有尝试EF4.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.

+1为缺少的参数获取NullPointerException仍然是错误的-我不确定是否有EF的连接站点,但如果有,请向他们报告!谢谢我所做的与您在这里所做的完全相同,但是我得到了您的“错误:异常已由调用的目标引发”,并且具有相同的堆栈跟踪。Migrate.exe似乎确实是质量最低的。相对路径似乎存在一些问题。我有一个类似的问题,但结果是我的代码(通过ConfigurationManager.ConnectionString)而不是需要虚拟配置文件的EF。我建议你再检查一下这个。