Getting Migrate.exe to work

后端 未结 2 652
星月不相逢
星月不相逢 2020-12-29 11:23

I have been struggling on executing EF Migrate.exe to work.

My Solution has couple of projects. The migrations and the entities live in the project Data. The contro

2条回答
  •  没有蜡笔的小新
    2020-12-29 12:00

    After reading this, this, and this

    I have (I think) what you need :

    1. If you use migrate.exe against a .NET 4 assembly you NEED to rename the Redirect.config available in packages\EntityFramework.5.0.0\tools to migrate.exe.config and copy this to the SAME directory as migrate.exe. For running migrate.exe against a .NET 4.5 assembly you DO NOT NEED this copy, the migrate.exe.config must not exist.
    2. The correct version of entity framework DLL must be in the SAME directory as migrate.exe. Correct version is packages\EntityFramework.5.0.0\lib\net40\ for running migrate.exe against a .NET 4 assembly. Correct version is packages\EntityFramework.5.0.0\lib\net45\ for running migrate.exe against a .NET 4.5 assembly
    3. If you specify /StartUpDirectory= do not specify the path for /assembly example : C:\Tools\migrate.exe some.dll /StartUpDirectory=C:\Project\bin\.
    4. If you don't specify a startup directory, then you need to specify the full path in the /assembly example : C:\Tools\migrate.exe C:\Project\bin\some.dll - In this scenario migrate.exe will not be able to load the some.dll's dependencies, unless you put all some.dll's dependencies and put it in the SAME directory as migrate.exe.
    5. If you put the migrate.exe in the same path as your some.dll, then migrate.exe will be able to use the same EntityFramework.dll which your app uses, and can load all dependencies, and can load the some.dll without any path like C:\Tools\migrate.exe some.dll
    6. If you put the migrate.exe in a separate tools folder like Im doing it needs the correct version of the EntityFramework.dll in the SAME directory as migrate.exe, it will need the /StartUpDirectory= clause, and you should specify the name of the assembly without the path like : C:\Tools\migrate.exe some.dll /StartUpDirectory=C:\Project\bin\
    7. Heres the powershell commmand I use :
    $SolutionPath = (Resolve-Path '..').Path
    $ToolsPath = "$SolutionPath\Build\Lib\"
    
    task db  { 
      $migrator = $ToolsPath + 'Migrations\migrate.exe'  
      $migrateCommand = "$migrator zasz_me.dll /StartUpDirectory=$SolutionPath\zasz.me\bin\ /connectionStringName:FullContext /startUpConfigurationFile:$SolutionPath\zasz.me\Web.config /verbose"
      Write-Host $migrateCommand
      Invoke-Expression $migrateCommand
    }
    

提交回复
热议问题