-
Notifications
You must be signed in to change notification settings - Fork 8
GettingStarted
In order to compile and run the API on your machine you will need :
- Visual Studio
- .NET 8
- A database server installed. The API is compatible with :
- A SQL Client, for example :
- SQL Server Management Studio for Microsoft SQL server
- pgAdmin for PostgreSQL
To continue the tutorial, you must first install the database server (Microsoft SQL Server or PostgreSQL) on your machine.
First you will need to modify the settings.
In order to do this, you have to :
- Open Visual Studio with the solution
- Open
appsettings.json
file inside BlogCore project (root of the project).
You will have to modify these fields :
"DatabaseProvider": "MsSQL",
"ConnectionStrings": {
"Default": ""
}
- Change
"DatabaseProvider": ""
to :-
"DatabaseProvider": "PostgreSQL"
for PostgreSQL -
"DatabaseProvider": "MsSQL"
for Microsoft SQL Server
-
- Change
"Default": ""
with your connection string.- By default it will be :
-
Server=.;Database=BlogCore;Integrated Security=True;
for Microsoft SQL server -
Host=localhost;Port=5432;Database=BlogCore;Username=postgres;Password=[YourPassword];"
for PostgreSQL. - you can change choose what you want for the name of the database, BlogCore isn't an obligation.
-
- By default it will be :
Now you have to generate the scripts needed to create and configure the database.
- Select BlogCoreAPI for start-up project inside Visual Studio
- Open inside Visual Studio the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console)
- Inside Package Manager Console :
- Select DBAccess inside Default project picklist
- Execute the command :
-
Add-Migration CreateInitialDatabase -Context MsSqlDbContext
for Microsoft SQL Server -
Add-Migration CreateInitialDatabase -Context PostgreSqlDbContext
for PostgreSQL
-
After the command has run there will be a folder created inside DBAccess project called Migrations which contain the migration scripts that will create the database.
- Still inside Package Manager Console, execute the command :
-
Update-Database -Context MsSqlDbContext
for Microsoft SQL Server -
Update-Database -Context PostgreSqlDbContext
for PostgreSQL
-
This will run the scripts and create the database and the tables. If everything goes well, you will have your database generated and configured.
You can open your SQL Client (SQL Server Management Studio or pgAdmin) and verify the database.
- Inside Visual Studio, Choose Debug or Release configuration and then compile the solution.
- Click on start button of Visual Studio
- A web page should open :
https://localhost:[Port]/Swagger/index.html
- To check the installation, execute
GET /categories
from the swagger for example. - You should have data inside the result.
- You can just remove the Migration folder inside DBAccess project and redo the previous instructions (save it before if you want to keep the previous database).
- Don't forget to change the settings inside appsettings.json for your new database as explained at the beginning.
- Of course, this is only valid for development / test purposes. In production, you will also have to export the data from the previous database to the new database.
You just have to execute inside inside the Package Manager Console of Visual Studio (Tools > NuGet Package Manager > Package Manager Console) :
- Create the scripts :
-
Add-Migration [NameOfYourMigration] -Context MsSqlDbContext
for Microsoft SQL Server -
Add-Migration [NameOfYourMigration] -Context PostgreSqlDbContext
for PostgreSQL
-
- Execute the scripts :
-
Update-Database -Context MsSqlDbContext
for Microsoft SQL Server -
Update-Database -Context PostgreSqlDbContext
for PostgreSQL
-
Warning : Be careful, make a backup if you do it for a database in production. The operation can be destructive.
BlogCore - 2021 - up to date