Is it possible to change the location of the EF Migrations "Migrations" folder?

shrichards picture shrichards · Dec 17, 2011 · Viewed 15.5k times · Source

By default, the add-migration command attempts to create the migration .cs file in

  • Project Root
    • Migrations

I'd like to store my migrations along with the rest of my EF-related code in the \Data folder of my project:

  • Project Root
    • Data
      • Migrations

With this structure, when I execute

PM> add-migration Migration1

in the NuGet console I receive the following error:

    System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\MyProjectRoot\Migrations\201112171635110_Migration1.cs'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
   at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding)
   at System.IO.File.WriteAllText(String path, String contents)

Is it possible to specify the location on disk that the migration file should be created when executing the add-migration command?

Answer

Roger picture Roger · May 31, 2012

In the configuration class constructor add this line:

this.MigrationsDirectory = "DirOne\\DirTwo";

The namespace will continue to be set as the namespace of the configuration class itself. To change this add this line (also in the configuration constructor):

this.MigrationsNamespace = "MyApp.DirOne.DirTwo";