Entity Framework
Entity Framework support is still in beta and is not suitable for production databases.
Background
Entity Framework (EF) is an ORM framework that acts as a wrapper around a database. It allows us to support multiple (non-MSSQL) databases without having to maintain migration and query scripts for each.
Our EF implementations currently support Postgres and mySQL.
Setting up EF databases
The workflow here is broadly the same as with the normal MSSQL implementation: set up the docker
container, configure user secrets, and run the scripts in the scripts folders against their
relating databases in chronological order.
Requirements
- A working local development server
- Docker
- A way to manage user secrets in the server project - see User Secrets Reference
- Database management software (e.g. pgAdmin4 for Postgres or DBeaver for mySQL)
- The
dotnetcli - The
dotnetcli Entity Framework Core tool
You can have multiple databases configured and switch between them by changing the value of the
globalSettings:databaseProvider user secret. You don’t have to delete your connection strings.
Postgres
In the
devfolder of your server repository, run:docker compose --profile postgres upAdd the following values to your API and Identity user secrets, changing information like root password as needed. If you already have these secrets, make sure you update the existing values instead of creating new ones:
"globalSettings:databaseProvider": "postgres",
"globalSettings:postgreSql:connectionString": "Host=localhost;Username=postgres;Password=example;Database=vault_dev;Include Error Detail=true",In the
devfolder run the following to update the database to the latest migration:pwsh migrate.ps1 -postgresOptional: to verify that everything worked correctly:
- Check the database tables to make sure everything has been created
- Run the integration tests from the root of your server project using
dotnet test. Note: this requires a configured MSSQL database. You may also need to also set up a MySql database for the tests to pass
MySql
If you are using an M1 machine, you need to add platform: linux/amd64 tag to the mysql service on
the docker-compose.yml file.
In the
devfolder of your server repository, run:docker compose --profile mysql upAdd the following values to your API and Identity user secrets, changing information like root password as needed. If you already have these secrets, make sure you update the existing values instead of creating new ones:
"globalSettings:databaseProvider": "mysql",
"globalSettings:mySql:connectionString": "server=localhost;uid=root;pwd=example;database=vault_dev",In the
devfolder run the following to update the database to the latest migration:pwsh migrate.ps1 -mysqlnotepwsh migrate.ps1 -allwill run migrations for all database providersOptional: to verify that everything worked correctly:
- Check the database tables to make sure everything has been created
- Run the integration tests from the root of your server project using
dotnet test. Note: this requires a configured MSSQL database. You may also need to also set up a Postgres database for the tests to pass
Generating EF Migrations
If you alter the database schema, you must create an EF migration script to ensure that EF databases keep pace with these changes. Developers must do this and include the migrations with their PR.
Instructions
Update your data model in
Core/Entitiesas desiredEnsure your user secrets in
APIhave the correct provider configured in thedatabaseProviderfield. The value should match the provider you are generating migrations for.Open a terminal in the root of the project you want to generate a migration for (either
util/PostgresMigrationsorutil/MySqlMigrations)Generate the migration. This should have the same name as the corresponding MSSQL migration (except for the date, which the the tool will prepend automatically):
dotnet ef migrations add [NAME_OF_MIGRATION]Generate the migration SQL script:
dotnet ef migrations script [LATEST_PREVIOUS_MIGRATION]Run the SQL script against your database
Save the script output in the
Scriptsfolder