Skip to content

Commit 11a4269

Browse files
📝Documentation for EF Core migrations (#548)
* adding migration documentation to readme * add Aspire CLI into dependencies and added both options for adding migrations * Add optional attribute to Aspire dependency Co-authored-by: Daniel Mackay [SSW] <2636640+danielmackay@users.noreply.github.com> --------- Co-authored-by: Daniel Mackay [SSW] <2636640+danielmackay@users.noreply.github.com>
1 parent 759ba8d commit 11a4269

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ This is a template for creating a new project using [Clean Architecture](https:/
104104
### Prerequisites
105105
- [Docker](https://www.docker.com/get-started/) / [Podman](https://podman.io/get-started)
106106
- [Dotnet 9](https://dotnet.microsoft.com/en-us/download/dotnet/9.0)
107+
- [.NET Aspire CLI](https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/setup-tooling) (optional)
107108

108109
### Installing the Template
109110

@@ -184,6 +185,42 @@ dotnet new ssw-ca --name {{SolutionName}} --output .\
184185
185186
3. Open https://localhost:7255/scalar/v1 in your browser to see it running ️🏃‍♂️
186187
188+
### EF Migrations
189+
Due to .NET Aspire orchestrating the application startup and migration runner, EF migrations need to be handled a little differently to normal.
190+
191+
#### Adding a Migration
192+
Adding new migrations is still the same old command you would expect, but with a couple of specific parameters to account for the separation of concerns. This can be performed via native dotnet tooling or through the Aspire CLI:
193+
194+
1. Run either of following commands from the root of the solution.
195+
196+
```bash
197+
dotnet ef migrations add YourMigrationName --project ./src/Infrastructure/Infrastructure.csproj --startup-project ./src/WebApi/WebApi.csproj --output-dir ./Persistence/Migrations
198+
```
199+
200+
```bash
201+
aspire exec --resource api -- dotnet ef migrations add YourMigrationName --project ../Infrastructure/Infrastructure.csproj --output-dir ./Persistence/Migrations
202+
```
203+
204+
#### Applying a Migration
205+
.NET Aspire handles this for you - just start the project!
206+
207+
#### Removing a Migration
208+
This is where things need to be done a little differently and requires the Aspire CLI.
209+
210+
1. Enable the `exec` function:
211+
212+
```bash
213+
aspire config set features.execCommandEnabled true
214+
```
215+
216+
2. Pass the EF migration shell command through Aspire from the root of the solution:
217+
218+
```bash
219+
aspire exec --resource api -- dotnet ef migrations remove --project ..\Infrastructure --force
220+
```
221+
> [!NOTE]
222+
> The `--force` flag is needed because .NET Aspire will start the application when this command is run, which triggers the migrations to run. This will apply your migrations to the database, and make EF Core unhappy when it tries to delete the latest migration. This should therefore be used with caution - a safer approach is to "roll forward" and create new migrations that safely undo the undesired change(s).
223+
187224
## Deploying to Azure
188225
189226
The template can be deployed to Azure via

0 commit comments

Comments
 (0)