This repo contains 3 diffrence type of varities on how someone can migrate and update the database based on the ef / efocre based contexts and entities. Three aproaches mentioned here are :
Project Sample | Description |
---|---|
stand-alone-data-project-sourcecode-based | standard old school way of copying all source files and then running ef tool commands |
stand-alone-data-project-customapp-based | customer application which reated a docker image which as soon as starts , does the data migration |
stand-alone-data-project-efbundle-based | The devops friendly *best way *where migration bundle is created and it gets deployed which doest the job. |
Each inidivusual folder has its own docker compose file , source code and docker file.
Docker compose build the project , creates docker image and then spin up the container.
if you are making changes to source code , please dont forget to refresh image or rebuild image when running docker compose via docker-compose up --build
Rest of the details and steps are as per each type of project and can be seen inside project's readme file.
This samples uses postgresql for illustration purpose (as ef core is compatible not just with mssql but various other dbs ) Thus the docker compose file contains the postgre db as well as its UI tool (pg admin) docker images.
Resource | Running on |
---|---|
postgressql db | http://localhost:5435 |
pg admin web | http://localhost:5050 |
- Open PG admin web( http://localhost:5050)
- On Main Web UI use credentials mentioned in docker compose file i.e username/email as
[email protected]
and password asroot
.
![](https://private-user-images.githubusercontent.com/5575617/273593845-c5030ba7-bf6e-4d3d-8bd6-e88b97daf2bd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzU5Mzg0NS1jNTAzMGJhNy1iZjZlLTRkM2QtOGJkNi1lODhiOTdkYWYyYmQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OWFjYjA2ZjY3NDJkMTVhNzNhNjc3MGMwMDA4Yjc1ZDQ0OTRlZTdiYWU0MjUwYmY2ZWVmZDE3YjY1NTQxYzU4YSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ._L_fMh33BlJ2IwzPd81BUR1Gm9_X5-X591ou3zr3Eis)
- Now , once inside the portal , connect to locally running postgresql by providing the container name(as db) and other details as per docker compose file i.e username
postgres
and password aspostgres
.
![](https://private-user-images.githubusercontent.com/5575617/273594008-60badd9a-6438-4ff4-9210-1e99bd72fae2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzU5NDAwOC02MGJhZGQ5YS02NDM4LTRmZjQtOTIxMC0xZTk5YmQ3MmZhZTIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NmFhNDU2NTUxNDA3ZWY5MGUxNzQ1ZjUyZTc3NDJmZDliNWJhOWVmZmE1NzdiM2I1OGEwOGQ1OWE2YmM3YTEwMCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.L7sjkZrWFqUtq41ta0DM5S18r5Gen9nwNw8aKsYL56g)
- connect to db , expand as per image and reach till schemas --> tables and you will be able to view tables and schemas.
- Lets say you are inside the
stand-alone-data-project-sourcecode-based
- open Developer powershell as shows below from VS
![](https://private-user-images.githubusercontent.com/5575617/273616939-8ed4e16a-5d42-4a22-8d47-8742da349367.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzYxNjkzOS04ZWQ0ZTE2YS01ZDQyLTRhMjItOGQ0Ny04NzQyZGEzNDkzNjcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YTcwYTc0Y2ZiOTg3NzY0ZWUzZDA2Yzg3NjMxNmFhY2EzNzVhZmEyMGEzMmEwZjkxNmE5ZmMwZTA5MDIzODY4MyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.ImnLInepZX8wLBJ9P9ZxLAliafGDBbOlwSaf3dR1aQg)
- Navigate to DataProject folder
- Run command to add initial migration
dotnet ef migrations add InitialDbMigration -c CustomerManagementDbContext -o Migrations
![](https://private-user-images.githubusercontent.com/5575617/273617020-0ca6d3af-af4e-4c78-9aed-9fcd5554e684.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzYxNzAyMC0wY2E2ZDNhZi1hZjRlLTRjNzgtOWFlZC05ZmNkNTU1NGU2ODQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDZhODJhMzY2ODVjNzBhZTQ5OTBiZDZjY2E3YTE4MjVjMzYzODAyMjhlMzFkMWM3NDAwNGJkM2QyZjcwNjM5NiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.5fYGYunhK4KT6jWCuSff7Hu1StRKaIGpTqq877fCv8U)
- It will create model snapshot and initial migration files.
- Now lets say you add one more column in model , so add migration for that
dotnet ef migrations add AddedNewColumn -c CustomerManagementDbContext -o Migrations
![](https://private-user-images.githubusercontent.com/5575617/273617180-9fe6a779-cc76-4e30-81d0-c1196f9fc6f2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzYxNzE4MC05ZmU2YTc3OS1jYzc2LTRlMzAtODFkMC1jMTE5NmY5ZmM2ZjIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZGM1ZjBkMTRjODdjZjM4YWFkZDFhYWUwOGI1MWFmY2ZjMDE1MzExOTljNjRiYzQxYTYzYmQ2MzE4ZGY5NDAxMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.6v57C4_aFBrqPOuQ1EhXbQ7A70zmmpEh_9IR0dvAvQ4)
-
You will see that this new migration is also added.
![](https://private-user-images.githubusercontent.com/5575617/273617234-9ffc4e85-c987-42f8-8abe-00e64ee290f0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzYxNzIzNC05ZmZjNGU4NS1jOTg3LTQyZjgtOGFiZS0wMGU2NGVlMjkwZjAucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MzMzOTk4YjYzMzJiNTMyY2EzM2FlNWFkMzNhMDgyZjE5M2Q3ZTQ3ODJhZTgzOWZiYTk2MGUyNTQ0ZGJkOTA3ZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.TZJBky_zH0ovmmqpwjlXcso-ZnkkH2I7LVli9JwGxeo)
- Now you can run docker compose and it will build and deploye those migrations.
![](https://private-user-images.githubusercontent.com/5575617/273617351-20f47153-c8e6-4758-a51e-1b8cd5e4c763.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzYxNzM1MS0yMGY0NzE1My1jOGU2LTQ3NTgtYTUxZS0xYjhjZDVlNGM3NjMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OWI4NTk5MDI0ZDI2YWYxYWNlNGU2MDY3NjFlMTJjZjk0MDI0MTM4M2MxNzJlZTliNjYxYzY2ZjZkMGMyYzFjYyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.3hGrTu2Dm8VXE0DjkzdaLRHWZ3OVtqEe8gOoK8lGSss)
- You will see that docker images are build , db image spin up and migration applied.
![](https://private-user-images.githubusercontent.com/5575617/273617907-b4e7b92e-9068-4391-add8-6725e5d9a3b5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTc1MzkzODMsIm5iZiI6MTcxNzUzOTA4MywicGF0aCI6Ii81NTc1NjE3LzI3MzYxNzkwNy1iNGU3YjkyZS05MDY4LTQzOTEtYWRkOC02NzI1ZTVkOWEzYjUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MDRUMjIxMTIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MjMxNTFjZTA4MjVjNWIwYzY2MWRiYTM2Y2IzYmE0YjIwMWU4OThjNDdlZjkwNWI5OTNiZjM1NzRiMzRjOWMxZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.e107PgMCT8-OPkFD2qZCsSZ9lMRoFq1z_Ni5ABNqLZc)
- This build steps , process and migration apply will very based on which type of above 3 mentioned project sample are you running.