1

Does anyone know how the location of the migrations folder is determined.

I added my data folders after the fact. But now I've ended up with this:

enter image description here

The Data\Migrations folder is where I'd like my migrations to go. But, as you can see, they're going in the Migrations folder off of the root.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 2
    Does this answer your question? [How to change the output folder for migrations with asp.net Core?](https://stackoverflow.com/questions/40696305/how-to-change-the-output-folder-for-migrations-with-asp-net-core) – Sir Rufo Feb 05 '22 at 02:32
  • How did you create your Migrations? – Charles Feb 05 '22 at 02:39
  • @Charles: I just created my model and `ApplicationDbContext` classes and used `add-migration`. – Jonathan Wood Feb 05 '22 at 03:11
  • @SirRufo: I don't want to have to type the folder every time I add or remove a migration. – Jonathan Wood Feb 05 '22 at 03:14
  • 1
    @JonathanWood That sounds like a world first problem. How often do you add or remove migrations that you can't store the line in a text file or as a comment in code somewhere. – Charles Feb 05 '22 at 07:51
  • @Charles: I don't follow. What are you trying to accomplish there? Currently, some method is used to determine the path used when I don't specify it. My question is about that method. – Jonathan Wood Feb 05 '22 at 14:54

1 Answers1

1

The migrations folder of the add migration commands is determined by the respective output directory argument of the command (--output-dir or -o for CLI dotnet ef migrations add and -OutputDir for PMC Add-Migration). And as explained in the documentation of the both commands:

The directory use to output the files. Paths are relative to the target project directory. Defaults to "Migrations".

So you have to provide that parameter, for instance

Add-Migration SomeMigration -OutputDir Data\Migrations

Note that this would also change the migrations classes namespace.

Now, the cool thing not mentioned in the documentation is that you have to do that only once, because the next add migration commands actually default to the last migration folder.

With that being said, the best is to specify this for the very first migration you are adding.

In case you want to modify the existing project with already added migrations like shown in the question, then manually move the whole root "Migrations" folder to "Data", then optionally change the namespace of all .cs files inside (including the .designer.cs) to namespace {RootNamespace}.Data.Migrations, and you are done. The next migrations will be added there without the need of specifying the folder.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • Thanks. I moved the existing migrations to the folder I wanted and changed all the namespaces accordingly. And when I created a new migration, it put it in the new folder and gave it the new namespace. So somehow it must search for some of those existing classes to determine where migrations should go. – Jonathan Wood Feb 05 '22 at 15:47
  • 1
    unicorn magic! – bricelam Mar 01 '22 at 18:35