ShopXPress API is an example Web API using Asp.Net Core which aim to use for training for people who interesting.
- .Net 8
- Entity Framework Core 8
- EF Core Tool
- Docker - optional
In case for development you can use docker to run the mysql8 database locally
This will start up the mysql service container that expose port to 3406
$ cd ./ShopXPress.Api/Docker/
docker-compose up
Then you can use following connectionstring inside appsettings.json
file
Server=localhost;Port=3406;Database=shopxpress-db;User=user;Password=password;
This project is implemented with Entity Framework Core built up with Code First
, so some command line are needed
Every time that we update or create or even delete the entity or property we need to create a migration to generate the sql script (in code) to update the Database changed.
Run the command
cd ShopXPress.Api
dotnet ef migrations add InitialDatabase -o "Entities/Migrations"
Every time that we changed the entity and created migrations then we need to apply the change to database by using following command. Only migration that hasn't applied will be execute.
$ cd ../ShopXPress.Api/Docker/
dotnet ef database update
There are 2 cases to revert migration
This case you can run the command to remove your recent migration that created recently.
You can use this command to revert the unapply migration
dotnet ef migrations remove
This this command will delete most recent migration and undo the database snapshot file.
If you've appplied the change already, you need to revert the change before remove the migration.
For example if you've apply the migrations named 20240217045714_UpdateUserTable_NotCorrect
(inside folder Migrations
)
and you want to revert this change then you need to run update database with your latest successful one.
Incase that the previous migration is 20230216035259_UpdateLatest
You will need to run update database with migration named 20240216035259_UpdateLatest
first.
dotnet ef database update 20240216035259_UpdateLatest
Then run remove migration again to remove the incorrect one
dotnet ef migrations remove
Shortcut To undo and remove last migration This command will remove and revert the last migration.
dotnet ef migrations remove --force