Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 440
shrichards Avatar asked Dec 17 '11 16:12

shrichards


People also ask

How do I change migration Assembly?

By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."

How do you deploy EF migrations?

Right click your web project, click publish, use web deploy, go to your databases, target your new database, ensure Execute Code First Migrations is checked (this will run all the migrations you've done for your localdb on your new database).


2 Answers

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"; 
like image 78
Roger Avatar answered Sep 23 '22 19:09

Roger


Specifying the migrations folder is also possible during the invoke of the enable-migrations command (which creates the Configuration class), using the -MigrationsDirectory parameter:

enable-migrations -EnableAutomaticMigration:$false -MigrationsDirectory Migrations\CustomerDatabases -ContextTypeName FullyQualifiedContextName 

The example will create a Configuration class which sets the MigrationsDirectory to the specified folder 'Migrations\CustomerDatabases' which is relative to the projects root folder.

public Configuration() {     AutomaticMigrationsEnabled = false;     MigrationsDirectory = @"Migrations\CustomerDatabases"; } 


See also this article which explains about a project with multiple contexts and migration folders.

By the way, if you are using multiple migrations folders and multiple contexts, please consider also to set up a name for the default schema in the OnModelCreating method of you DbContext derived class (where the Fluent-API configuration is). This will work in EF6:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)     {         modelBuilder.HasDefaultSchema("CustomerDatabases");     } 

The will prefix you database tables with the schema name. This will enable you to use more than one context with a single database in a scenario where you have several groups of tables which are independent from another. (This will also create separate versions of the MigrationHistory tables, in the example above it would be CustomerDatabases.__MigrationHistory).

like image 36
Martin Avatar answered Sep 20 '22 19:09

Martin