The focus of the exercise is on: connecting to an external database; defining models, relations and migrations; writing some basic tests and implementing DTOs.
Create a new appsettings.json / appsettings.Development.json (see appsettings.Example.json) add the following json and update credentials
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnectionString": "Host=HOST Database=DATABASE; Username=USERNAME; Password=PASSWORD; "
}
}
-
Install the following packages into the WebApi project:
-
Install-Package Microsoft.EntityFrameworkCore
-
Install-Package Microsoft.EntityFrameworkCore.Tools
-
Install-Package Microsoft.EntityFrameworkCore.Design
-
Install-Package NpgSql.EntityFrameworkCore.PostgreSql
dotnet new gitignore
Then add the following to the bottom of the file:
/workshop.wwwapi/appsettings.json
/workshop.wwwapi/appsettings.Development.json
*/**/bin/Debug
*/**/bin/Release
*/**/obj/Debug
*/**/obj/Release
Add-Migration MIGRATION_NAME
Update-Database
Examine the test project. This is a a standard NUnit Test project (dotnet new nunit --name workshop.tests
) with the additional package:
Install-Package Microsoft.Aspnecore.Mvc.Testing
Ensure that the following line is added to the bottom of the WebApi project:
public partial class Program { } // needed for testing - please ignore