在没有Web / App.config的情况下运行实体框架migrate.exe工具的正确格式是什么?

时间:2021-10-03 14:10:54

We recently switched to Entity Framework data migrations and I am working on some build automation scripts for our MVC app. I can successfully run the migrations from our build server using the migrate.exe tool in 4.3 if I have a Web.config to point it at. The command looks something like:

我们最近切换到Entity Framework数据迁移,我正在为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

However, for various reasons I would like to avoid using the Web.config and just pass in the correct connection string at the time of the migration:

但是,出于各种原因,我希望避免使用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

This does not work. Worse, it crashes migrate.exe with a NullReferenceException. The connection string is identical to the one we use in our Web.config.

这不起作用。更糟糕的是,它使用NullReferenceException崩溃migrate.exe。连接字符串与我们在Web.config中使用的字符串相同。

Anyone encountered this before? Is my connection string format wrong? Bug?

以前遇到过这个人吗?我的连接字符串格式错了吗?错误?

2 个解决方案

#1


23  

Ok, we figured it out. When running without the Web.config, the connectionProviderName parameter must also be passed in:

好的,我们弄清楚了。在没有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

I have confirmed that this works.

我已经确认这是有效的。

#2


12  

I have yet to find a solution that actually works without specifying the web/app.config file. See below.

我还没有找到一个实际工作的解决方案,而没有指定web / app.config文件。见下文。

However, if you can accept providing a web/app.config and overriding the connection string as command-line parameters, then the following works with Entity Framework 5.0 nuget and .NET 4.5. Should also work for .NET 4.0 with the documented workarounds.

但是,如果您可以接受提供web / app.config并将连接字符串覆盖为命令行参数,则以下内容适用于Entity Framework 5.0 nuget和.NET 4.5。应该也适用于带有记录的变通方法的.NET 4.0。

Example folder structure:

示例文件夹结构:

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:

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

End of solution.

解决方案结束。


Omitting the configuration file:

省略配置文件:

If trying to omit the configuration file, I always got the following exception no matter what I tried. I didn't try EF 4.3, so I suspect the behavior changed between 4.3 and 5.0.

如果试图省略配置文件,无论我尝试什么,我总是得到以下异常。我没有尝试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.

#1


23  

Ok, we figured it out. When running without the Web.config, the connectionProviderName parameter must also be passed in:

好的,我们弄清楚了。在没有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

I have confirmed that this works.

我已经确认这是有效的。

#2


12  

I have yet to find a solution that actually works without specifying the web/app.config file. See below.

我还没有找到一个实际工作的解决方案,而没有指定web / app.config文件。见下文。

However, if you can accept providing a web/app.config and overriding the connection string as command-line parameters, then the following works with Entity Framework 5.0 nuget and .NET 4.5. Should also work for .NET 4.0 with the documented workarounds.

但是,如果您可以接受提供web / app.config并将连接字符串覆盖为命令行参数,则以下内容适用于Entity Framework 5.0 nuget和.NET 4.5。应该也适用于带有记录的变通方法的.NET 4.0。

Example folder structure:

示例文件夹结构:

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:

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

End of solution.

解决方案结束。


Omitting the configuration file:

省略配置文件:

If trying to omit the configuration file, I always got the following exception no matter what I tried. I didn't try EF 4.3, so I suspect the behavior changed between 4.3 and 5.0.

如果试图省略配置文件,无论我尝试什么,我总是得到以下异常。我没有尝试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.