Skip to content

Commit

Permalink
XS✔ ◾ EF Core Migrations Rule (#7593)
Browse files Browse the repository at this point in the history
* Updated rule

* Auto-fix Markdown files

* Fix MD lint errors

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
danielmackay and github-actions[bot] authored Dec 18, 2023
1 parent afa4797 commit 2ecadb3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
Binary file added rules/use-code-migrations/rider-ef-core.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 73 additions & 10 deletions rules/use-code-migrations/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,85 @@ type: rule
title: Do you know to use code migrations
uri: use-code-migrations
authors:
- title: Bryden Oliver
url: https://www.ssw.com.au/people/bryden-oliver
- title: Daniel Mackay
url: https://www.ssw.com.au/people/daniel-mackay
related: []
created: 2021-12-13T17:27:38.786Z
guid: 8284cedd-8eea-4e3b-b04b-451896a615c0
---
::: todo
TODO: Byrden
:::


Most enterprise applications require a database to store data. Once you have a database you need a way to manage the schema.

Entity Framework Code First Migrations allow you to update a database schema rather than recreate it from scratch. This is useful when you have a production database that you want to keep, but you want to make changes to the schema.

<!--endintro-->

::: bad
Bad example - Don't modify database directly
## Database Schema Management Options

1. Manually modify the database schema (not recommended)
2. Manually creating SQL scripts (not recommended)
3. DB Up
4. Entity Framework Code First Migrations (recommended)

## Configuring Entity Framework Code First Migrations

The following assumes you have an existing project with a database context, entities, and have already installed the EF Core nuget packages.

1. Install EF Core Tools

```bash
dotnet new tool-manifest
dotnet tool install dotnet-ef
```

2. Enable migrations

```bash
dotnet ef migrations add InitialCreate
```

3. Update database

```bash
dotnet ef database update
```

### Using Rider

If you struggle to remember the commands above, Rider has a great UI that makes creating migrations easy. This is especially useful when you have different projects for both startup and migrations.

::: img-large
![Rider - EF Core Migrations](rider-ef-core.png)
:::

## Executing Entity Framework Code First Migrations

Once you have some migration, you'll then need to decide when these get run. Naively, developers will often run migrations during program start-up, but this is not recommended. Doing so can cause issues in a web farm environment, as well as cause unnecessary delays during start-up.
::: bad
```csharp
var dbContext = scope.ServiceProvider.GetRequiredService<EagleEyeDbContext>();
await dbContext.Database.MigrateAsync();
```
Figure: Running migrations manually during startup in `program.cs`
:::
### Entity Framework Migration Bundles
A place to run migrations is during your CICD deployment pipeline.
::: good
Good example - modify entities and generate a migration
:::
```bash
dotnet ef migrations bundle --self-contained --force
.\efbundle.exe --connection {$ENVVARWITHCONNECTION}
```
Figure: Creating and executing a migration bundle during a CICD pipeline
:::
::: info
if an `appsettings.json` file can be found the connection string can be automatically picked up.
:::

0 comments on commit 2ecadb3

Please sign in to comment.