Getting Migrate.exe to work

12

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 controllers and views live in Web.

I tried using the migrate.exe - however I am struggling getting the first argument (assembly) to be accepted. Documentations says:

Assembly: Specifies the name of the assembly that contains the migrations configuration type.

I have tried:

migrate.exe "MySolution\DataProject\bin\Debug\Data.dll"

ERROR: Could not load file or assembly 'D:\\MySolution\\Data\\bin\\Debug\\Data' or one of its dep
endencies. The given assembly name or codebase was invalid. (Exception from HRES
ULT: 0x80131047)

Any idea what is going wrong?

asp.net-mvc
entity-framework-4
migration
asked on Stack Overflow Aug 3, 2012 by Karan

2 Answers

33

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=<the path where you target dll is present> 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
}
answered on Stack Overflow Sep 4, 2012 by Zasz • edited May 22, 2014 by jgillich
1

I answered a similar question here on how to override connectionstring through parameters to migrate.exe. I have yet to get it working without specifying a web/app.config file.

https://stackoverflow.com/a/14138797/134761

answered on Stack Overflow Jan 3, 2013 by angularsen • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0