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

后端 未结 2 1322
说谎
说谎 2021-01-31 02:08

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

  • Project Root
    • Migrations

I\'d like to store my

2条回答
  •  盖世英雄少女心
    2021-01-31 02:42

    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).

提交回复
热议问题